On mu Ubuntu 12.04 setups my tmux clipboard copy and paste commands are setup as follows:
set -g prefix M-a unbind C-b bind C-c run "tmux save-buffer - | xclip -i -sel clipboard" bind C-v run "tmux set-buffer "$(xclip -o -sel clipboard)"; tmux paste-buffer"
This worked great for the longest time up until a month or so ago, when I suspect some configuration change or package change was made that broke the above. In GNOME terminal, pasting still works fine with both prefix+ctrl-v and ctrl-shift-v.
However the xclip copy command no longer works no matter what I do, and I have tried removing the custom prefix binding above, using -select instead of -sel, not using clipboard etc. This is pretty much a show stopper for a GVim user like me, since I don’t even have the GNOME terminal workaround of ctrl-shift-c with tmux taking over the shell. I go into copy mode, select text with space+movement, and when I execute prefix+ctrl-c absolutely nothing happens. Before this broke, tmux would display a confirmation message in the notification section at the bottom.
Does anybody have suggestions as far as how one might debug this? This is a pretty big productivity hit. I can probably use the temporary file workaround trick for now, but it’d be great to know just what happened to xclip.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The xsel utility is similar to xclip, but implemented a little differently. Normally I would expect them to behave in the same way, but they don’t make exactly the same X library call, so it’s possible that in some corner cases xsel will work but not xclip, or vice versa. Try:
bind C-c run "tmux save-buffer - | xsel -ib" bind C-v run "tmux set-buffer "$(xsel -ob)"; tmux paste-buffer"
Method 2
Adding -b to run-shell (or run) command fixed the problem. With -b the shell command is run in the background.
bind C-c run-shell -b "tmux save-buffer - | xclip -i -sel clipboard"
Method 3
Although I can’t reproduce it anymore but here’s the technical answer what might have happened in your case.
First, you need to understand how X11 clipboard works. You might read jwz’s essay on this: http://www.jwz.org/doc/x-cut-and-paste.html
In short, the application which holds the contents of the clipboard needs to run until other application asserts the ownership. So when you run xclip -i <<< test then you can see xclip running in the background until you make another selection:
$ xclip -i <<< test $ ps PID TTY TIME CMD 10166 pts/8 00:00:00 xclip 10171 pts/8 00:00:00 ps 19345 pts/8 00:00:00 bash
Now this is all fine but when you exit this shell then all processes belonging to this session are killed by default by sending them a HUP signal. This will mean xclip will be killed and you won’t be able access your clipboard contents anymore.
So the suggested workaround (in case you don’t have xsel) is to ignore the HUP signal by using the following bind:
bind C-c run "tmux save-buffer - | nohup >/dev/null 2>/dev/null xclip -i -sel clipboard"
xsel is not affected by this problem because the first thing it does after the fork() is to disassociate itself from the controlling terminal so it won’t receive the HUP signal when its shell exits (you won’t even see it in the above ps output but only when you do a ps -e | grep xsel).
Method 4
I’m experiencing a similar problem and the temporary file won’t help in this particular case, I’m afraid. This is because xclip seems to behave differently when spawned by tmux than when being run “interactively” and waits for another application to take ownership of the clipboard area. Try using xclip -l 1 to make it quit immediately (see man page for details).
Method 5
This is an old question, but I suspect I have the solution, taken from 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 your command should become:
bind C-c run "tmux save-buffer - | xclip -i -sel clipboard >/dev/null"
Method 6
This is a working configuration I use:
# Yank to copy text with y. bind-key -t vi-copy y copy-pipe "tmux save-buffer - | xclip -sel clipboard -i" # Update default binding of `Enter` to also copy with this method. unbind -t vi-copy Enter bind-key -t vi-copy Enter copy-pipe "tmux save-buffer - | xclip -sel clipboard -i" # Toggle rectangular copy mode. bind-key -t vi-copy 'C-v' rectangle-toggle # Bind ']' to paste. bind ] run "tmux set-buffer "$(xclip -o -sel clipboard)" && tmux paste-buffer" # Toggle rectangular copy mode. bind-key -t vi-copy 'C-v' rectangle-toggle # http://askubuntu.com/a/507215/413683 set -g set-clipboard off
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0