This is my rather lengthy answer that should fix two major problems with using xclip to copy the buffer from a remote (ssh) tmux session:
1. Irresponsive xclip
For me @Grauwolf's answer with xclip
didn't work (renders tmux pane totally irresponsive).
I found out why in the Tmux page of the Arch wiki:
xclip could also be used for that purpose, unlike xsel it works better on printing raw bitstream that doesn't fit the current locale. Nevertheless, it is neater to use xsel instead of xclip, because xclip does not close STDOUT after it has read from tmux's buffer. As such, tmux doesn't know that the copy task has completed, and continues to wait for xclip's termination, thereby rendering tmux unresponsive. A workaround is to redirect STDOUT of xclip to /dev/null
So the binding should be:
bind-key C-y run "tmux save-buffer - | xclip -i -sel clip > /dev/null"
Now, this will work if your tmux session is local.
2. xclip unable to connect to X
If you are using tmux over ssh -X, there are big chances this won't work directly.
That's because the shell variable $DISPLAY
needs to be set properly.
Complete solution
So for me, the complete working solution is to put the following lines in my ~/.tmux.conf
:
set-option -g update-environment "DISPLAY"
bind-key C-y run "export DISPLAY=`tmux show-env | sed -n 's/^DISPLAY=//p'`; tmux save-buffer - | xclip -i -selection clipboard >/dev/null"
# Or more concise:
bind-key C-y run "tmux save-buffer - | xclip -i -selection clipboard -d `tmux show-env | sed -n 's/^DISPLAY=//p'` >/dev/null
For pasting from primary:
bind-key C-p run "xclip -d `tmux show-env | sed -n s/^DISPLAY=//p` -o | tmux load-buffer - && tmux paste-buffer"
I don't fully understand why $DISPLAY
isn't correctly set while sourcing .tmux.conf
, so this is why I have to extract it with tmux show-env
and sed
.
If you have tmux >= 1.8, you can adapt this command with copy-pipe
:
bind-key -t vi-copy y copy-pipe 'xclip -in -selection clipboard -d `tmux show-env | sed -n s/^DISPLAY=//p` >/dev/null'