Unable to use pbcopy while in tmux session

Running tmux 1.4 installed from ports on snow-leopard I am unable to use the built in OSX pbcopy command.

Outside of tmux:

> echo "abc" | pbcopy
> echo $(pbpaste) # or using ^v
abc

But inside of tmux:

> echo "123" | pbcopy
> echo $(pbpaste)
abc

I've scoured the man page but can't find any options that might relate to this behavior. I also can't understand why tmux would mess with shell redirection.

Anybody have any clues?


Solution 1:

Using homebrew

brew install reattach-to-user-namespace --with-wrap-pbcopy-and-pbpaste

or Macports

sudo port install tmux-pasteboard

Prepend the following to the top of ~/.tmux.conf

set-option -g default-command "reattach-to-user-namespace -l zsh"

You might want to swap out the zsh for whichever shell you are using, for portability you should probably use $SHELL.

Taken from => robots.thoughtbot.com

Solution 2:

When tmux detaches from the console (in order to support attach/detach), it does so in a way that doesn't allow it to communicate with the GUI any more.

Apple's patches to GNU screen use a private system call to detach in a different way. There is a patch in the Macports bug tracker that makes pbcopy/pbpaste work again. I also submitted it to the Homebrew repo, where it was briefly accepted, then reverted.

This is because when this patch is applied, it causes tmux to crash when re-attaching to a session.

See the pull request on github for the Homebrew project for more info.

Solution 3:

I found a github project that wraps tmux so that pbpaste and pbcopy work and doesn't have the issues described that caused Homebrew to remove the patch. It's pretty easy to install, just a small bit of compilation and one line in your .tmux.conf

Homebrew had a patch, but as others mentioned, it was backed out because it caused issues with reattaching.

There was an issue filed against tmux, but it was closed because the tmux author doesn't have a Mac to test on. The README on the github tmux-MacOSX-pasteboard project has great details on why patching tmux is difficult in this case, and a bunch of other info surrounding the situation. It works on OSX 10.6 for me and reading through the code looks like it will work for 10.5 as well.