Macos – Synchronize pasteboard between remote tmux session and local Mac OS pasteboard

macossshtmuxvim

Setup:
I use iTerm2 on MacOS to connect to a remote server.
The remote server runs tmux, in which I open files and edit in vim sessions.

Problem:
I can't copy/paste between the remote tmux session and the local iTerm client. I can use iTerm 2's alt/option + mouse selection to select text, but this copies over multiple vim panes/tmux panes – bad.

Is there any elegant solution to make selections in tmux panes synchronize between the remote pasteboard and the local (MacOS pasteboard)? I've seen reattach-to-user-namespace, but I'm pretty certain it doesn't do what I want.

Best Answer

So, when you ssh into the server, you can use reverse tunneling so that you can talk back to your OSX machine to send it commands do pbcopy.

ssh -R 1234:localhost:22 remoteServer

Replace 1234 with any open port the remote server. Then on the remote server, you can then run:

tmux save-buffer - | ssh -p 1234 localhost pbcopy

That should connect back to OSX and send the contents of your tmux copy buffer to it. You may want to use ssh keys to prevent typing your password to your OSX machine.

If that works for, you can then create your tmux keybindings.

bind C-c run "tmux save-buffer - | ssh -p 1234 localhost pbcopy"

I should note, for this to work, you will need to turn on Remote Sharing in OSX.

Secondary Option:

Since you're using OSX, if you're also using iTerm2, you might consider installing http://code.google.com/p/iterm2/downloads/detail?name=tmux-for-iTerm2-20120203.tar.gz&can=2&q= on your servers.

It's basically a custom tmux what supports the "-C" flag. This flag hands over windows, and panes and all their splitting to iTerm2.

Related Question