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?


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
  • 6
    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) – RubenLaguna May 28 '15 at 11:26
  • 4
    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
  • 1
    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

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"
  • 6
    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

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

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

I tried a bunch of stuff here and there, but this is what worked form me in my ~/.tmux.conf file I am using Ubuntu 20 LTS and tmux3.0a

#for copying to sys clipboard
bind -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "xclip -i -f -selection primary | xclip -i -selection clipboard"
bind -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -i -f -selection primary | xclip -i -selection clipboard"
bind -T copy-mode-vi C-j send-keys -X copy-pipe-and-cancel "xclip -i -f -selection primary | xclip -i -selection clipboard"

#general other stuff
set -g default-terminal "xterm-256color"
set -g mouse on
set-window-option -g mode-keys vi
  • wow thanks, found this after 100 obsolete answers. surprised their api has changed so many times – Jeff Puckett Oct 29 '20 at 21:17
  • Nice answer. Catch 22: You need to restart tmux completely (using tmux kill-server) after making changes to ~/.tmux.conf. – PhoemueX Dec 3 '20 at 9:49

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

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

  • 1
    macOS users, for tmux >= 2.5: bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'pbcopy' – theaws.blog Oct 24 '19 at 3:38

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.


On Wayland, instead of xclip, better use wl-copy/wl-paste of wl-clipboard package. This works for me:

set-window-option -g mode-keys vi
bind-key -T copy-mode-vi 'v' send-keys -X begin-selection
bind-key -T copy-mode-vi 'y' send-keys -X copy-pipe-and-cancel "wl-copy"
bind C-p run "wl-paste --no-newline | tmux load-buffer - ; tmux paste-buffer"

update: since I use the same tmux.conf on X11, Wayland, and Cygwin, I ended up with this configuration:

bind-key -T copy-mode-vi v send -X begin-selection

if-shell -b 'echo $XDG_SESSION_TYPE | grep -q x11' "\
    bind-key -T copy-mode-vi 'y' send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard > /dev/null'; \
    bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard > /dev/null'; \
    bind-key C-p run 'xclip -out -selection clipboard | tmux load-buffer - ; tmux paste-buffer'"

if-shell -b 'echo $XDG_SESSION_TYPE | grep -q wayland' "\
    bind-key -T copy-mode-vi 'y' send-keys -X copy-pipe-and-cancel 'wl-copy'; \
    bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel 'wl-copy'; \
    bind-key C-p run 'wl-paste --no-newline | tmux load-buffer - ; tmux paste-buffer'" "\
    bind-key -T copy-mode-vi 'y' send-keys -X copy-pipe-and-cancel 'cat - >/dev/clipboard'; \
    bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel 'cat - >/dev/clipboard'; \
    bind-key C-p run 'cat /dev/clipboard | tmux load-buffer - ; tmux paste-buffer'"

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.