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.

  • Is xclip actually available? What is the output of type xclip? – Chris Down May 28 '13 at 5:18
  • The output is: "xclip is /usr/bin/xclip". As I mentioned, pasting through xclip works just fine for whatever reason. – Alexandr Kurilin May 28 '13 at 5:20
  • 1
    Do you have the same problem with xsel -b? – Gilles May 29 '13 at 7:34
  • @Gilles, xsel -i -b does seem to do the trick! – Alexandr Kurilin May 29 '13 at 20:30
  • Heh. I have no idea why it works, I thought xsel -b and xclip -selection clipboard would do the same thing! – Gilles May 29 '13 at 21:45
up vote 8 down vote accepted

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"
  • The same result - do not works for me - works as standalone command but return empty string from such bind; Ubuntu 14.04, xsel 1.2.0, tmux 1.8-5 – Vitaly Zdanevich Mar 11 '17 at 6:58

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).

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).

  • Can't seem to get that work work. Is this what that binding should look like: bind C-c run "tmux save-buffer - | xclip -l 1 -i -sel clipboard" ? – Alexandr Kurilin May 28 '13 at 17:41
  • Could you perhaps post what workaround worked for you? That'd be pretty helpful as well! – Alexandr Kurilin May 28 '13 at 17:51
  • I'm using bind C-y run-shell "tmux save-buffer - | xclip -l 1 -i". – peterph May 28 '13 at 21:04
  • Yeah, still nothing unfortunately. Tmux doesn't report anything, nor does xclip. – Alexandr Kurilin May 29 '13 at 0:17
  • For me it's not reporting anything either, but it works. Check what xclip is doing when you try to copy the buffer. If it stays hanging there waiting for something, does killing it help? Also, you have to exit the selection mode by hitting Enter (and hence storing it into buffer) before you try to copy the buffer into the X clipboard. – peterph May 29 '13 at 9:36

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"

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

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.