46

I'm running tmux 1.6 and I'm trying to configure it to use vi-style keybindings as well as use the system clipboard when copying in interactive mode:

set-window-option -g mode-keys vi

bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' "copy-selection && run \"tmux save-buffer | xclip -selection clipboard\""

Simply put, I'd like to be able to do C+[ and then use v to begin selecting text for copying, then when y is pushed, copy the selection to the tmux selection and then export it to the system clipboard using xclip.

Unfortunately, when I try to do this, I see the following:

.tmux.conf: 14: unknown command: copy-selection && run "tmux save-buffer | xclip -selection clipboard"

Is there a way to do this in tmux configuration?

80

This was also answered here, but it took me a while to understand how to use it, so I'll explain for anyone else that was confused.

This is basically the setting you're going for:

(for tmux versions <2.5)

bind -t vi-copy y copy-pipe 'xclip -in -selection clipboard'

(for tmux versions >=2.5)

bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard'

Then hit Ctrl+b [ to enter copy mode. Then hit Space followed by whatever vi movement keys to make a selection. Then, instead of hitting Enter, hit y and the selection will be copied to the clipboard.

Note: this assumes you're using tmux's default bindings with vi keys.

Tmux has different key binding tables for different modes. So, bind-key -t vi-copy y sets the action for the y key in copy mode. Initially, I was confused because I was used to hitting Enter after making a selection. Enter is actually just the default key binding for the copy-selection command (when in copy mode). The copy-pipe command allows us to bind a new key to pipe the selection to a command, which in this case is xclip.

You can see all key bindings for copy mode by running list-keys -t vi-copy.

  • Thanks so much, I had to download and compile tmux 1.9a, but it wasn't so difficult and now it just works™! – Naftuli Kay May 21 '14 at 17:02
  • 3
    If it doesn't work just double check that xclip is actually installed in your system (it's not installed by default on Ubuntu, you need to sudo apt-get install xclip to get it) – ecerulm May 28 '15 at 11:26
  • 2
    For OSX, replace xclip with pbcopy. For Cygwin on Windows, replace it with putclip (from the cygutils-extra package). – SnoringFrog Jun 17 '16 at 15:09
  • 5
    The following binding from this blog entry works great too on Tmux 2.6 and doesn't have the downside of putting seemingly random characters on the screen when copying: bind -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "xclip -i -f -selection primary | xclip -i -selection clipboard" – Matthias Braun Nov 30 '17 at 19:30
  • This was not working for me until I removed this line from .tmux.conf: set -g mouse on. This is supposed to enable "mouse mode" but even after removing it everything regarding my mouse works fine (scroll wheel works, mouse selection works)... – ndemou Jun 15 '18 at 10:32
10

The updated version to Ben Davis answer

compatible with tmux 2.4

bind -T copy-mode-vi y send -X copy-pipe "xclip -selection c"
  • 4
    I find it works a bit better with copy-pipe-and-cancel rather than copy-pipe. This is on tmux 2.5 – wreckgar23 Jul 22 '17 at 23:02
6

You are running into a couple of technical issues:
The “special mode” bindings (e.g. vi-copy) …

  1. use a different set of commands (i.e. run-shell a.k.a. run is not available), and
  2. do not have the ability to bind multiple commands (which can normally be separated by ;—the shell’s && command separator is not available).

tmux 1.8 has copy-pipe, which could help though:

bind-key -t vi-copy y copy-pipe 'xclip -selection clipboard >/dev/null'

(There is a binding like this listed in the FAQ.)

  • 1
    I have tmux 1.8 and this doesn't seem to work. (edit) Ugh, I hate textarea inputs that submit on Enter. Anyway, I added this key binding, but when I do <prefix>-y, and hit [space], the contents are not copied to the clipboard. – Ben Davis May 20 '14 at 16:04
  • Just figured it out. I didn't understand how the "vi-copy" keybinding table worked. – Ben Davis May 20 '14 at 19:28
5

Take a look at the tmux-yank plugin for tmux. https://github.com/tmux-plugins/tmux-yank

It provides an automated way to copy/paste from tmux to the system clipboard. It works on OSX/Linux/Cygwin systems.

  • This worked great for me, thanks for the link! – Bryan Henry Jul 31 '17 at 20:58
2

For macOS you can add this line to .tmux.conf

bind -t vi-copy y copy-pipe 'pbcopy'

0

This is what ended up working for me using tmux 2.2 and having installed xclip. For Vim style copying add the following to .tmux.conf

bind Escape copy-mode
bind -t vi-copy 'v' begin-selection
bind -t vi-copy 'y' copy-selection
# Vim style copy to clipboard
bind-key -t vi-copy y copy-pipe "xclip -selection c > /dev/null"
bind-key p run "xclip -o -sel clip | tmux load-buffer - ; tmux paste-buffer"

Here when in copy-mode PREFIX v starts the selection and PREFIX y copies to the clipboard. PREFIX p can be used to paste in the terminal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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