tmux xclip copy no longer working
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"
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"
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
).