30

In order to copy text to X clipboard in tmux, what I currently do is:

  • go selection mode with prefix[
  • start selection with space (using mode-keys vi btw)
  • select the text and press enter
  • copy tmux buffer to X clipboard using prefixctrl+c

(I have a mapping as bind C-c run "tmux show-buffer | xclip -selection clipboard -i"

I'm trying to eliminate some of these steps by making a mapping for selection mode. I have tried:

bind-key -t vi-copy C-c run "tmux show-buffer | xclip -selection clipboard -i"

which gives me and error:

.. usage: bind-key [-cnr] [-t key-table] key command [arguments]

(I couldn't copy the error)

how can I fix this?

EDIT: I have realized I can select and copy to tmux buffer (first 3 steps) using mouse just like a regular selection (although highlighting doesn't last, it still selects the text) and then use the following bind to copy to x clipboard:

bind-key -n C-c run "tmux show-buffer | xclip -selection clipboard -i"

(I used this method to copy this text and it was easy ;)

note that -n denotes "no prefix" therefore no escaping is needed. if you don't get confused by the vanishing highlight this is analogous to regular (common user interface) copying.

this is the best I have come up with so far..

EDIT2: turned out ctrl+c overlaps with process interrupt thing so I changed to ctrl+alt+c for now. (didn't quite like it)

EDIT3: tmux 1.8 or so added support to add keybindings in copy mode so now my first original intention is possible using something like:

bind-key -tvi-copy y copy-pipe "xclip -selection clipboard -i"
bind-key -tvi-copy enter copy-pipe "xclip -selection clipboard -i"

These two bindings make it possible to copy text to clipboard when I used enter or y to finish copying.

I still keep this line in case I do the copying with mouse and decide to get the tmux buffer content to clipboard later on:

bind-key y run "tmux show-buffer | xclip -selection clipboard -i"
8
  • 1
    You can change interrupt from ctrl-c to something else using stty. For example, to use Ctrl-Z: stty intr=^Z. Be careful with that command, it's possible to set a regular character to be the interrupt character (if you do so, use ctrl-v to enter it until fixing the setting). And don't get confused on what key is used (e.g. ctrl-z is normally the suspend key).
    – ash
    Sep 5, 2013 at 6:20
  • @ash noted down, thanks.. but I think I will keep ^c as the interrupt key as I use it more often than copying. I have also updated the answer to reflect my current solution to this problem.
    – none
    Sep 5, 2013 at 10:22
  • sounds good. Also note that many runs of xmodmap can put things in a funny state that is hard to correct, so I recommend starting fresh with a new X session if the affect of commands appears incorrect.
    – ash
    Sep 5, 2013 at 15:41
  • running bind-key -t vi-copy C-c run "tmux show-buffer | xclip -selection clipboard -i" live gives: Unknown command: run Dec 24, 2013 at 17:14
  • 1
    However, running bind-key C-c run "tmux show-buffer | xclip -selection clipboard -i" works just fine. Dec 24, 2013 at 17:20

2 Answers 2

19

This reply doesn't answer directly your question about creating a shortcut. But here's what I do.

You can temporarily suspend passing your keys/mouse events to tmux by holding shift. So you can press and hold Shift and user regular shortcuts of terminal/X to do the copy. In my case using Terminator, I press shift, highlight with mouse whatever I want, copy with Ctrl+Shift+C

And in case you have split panes, you can zoom the current pane with zoom-toggle-key Prefix + z, and perform the copy operation. (tested on tmux v1.8)

3
  • I wasn't able to use this because of horizontal splits but zooming trick sounds great, thanks for the tip.
    – none
    Feb 3, 2014 at 8:53
  • This works in gnome-terminal, for sure; but does it apply to all terminal-emulators? Probably not
    – jpaugh
    Sep 5, 2017 at 19:38
  • In wsl-terminal, this works. Save my day!
    – Qinsi
    Jul 15, 2019 at 7:20
2

This doesn't directly answer your question, but have a look at tmux-yank, as described in Use system clipboard in vi-copy mode in tmux and Getting tmux to copy a buffer to the clipboard. After installing,

prefix + y - copies text from the command line to clipboard.

copy mode bindings:

y - copy selection to system clipboard

1
  • I don't have tmux-yank installed, but since a while (maybe 2 month) this kind of shortcut is distracting me very much -> prefix + c (while holding ctrl) is inserting the clipboard - and this sometimes comes in my way when I want to create a new window with prefix + c (not holding ctrl) - I wonder why that is
    – MacMartin
    Jan 22 at 10:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .