95

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?

9 Answers 9

151

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.

14
  • 1
    Thanks so much, I had to download and compile tmux 1.9a, but it wasn't so difficult and now it just works™! May 21, 2014 at 17:02
  • 11
    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) May 28, 2015 at 11:26
  • 6
    For OSX, replace xclip with pbcopy. For Cygwin on Windows, replace it with putclip (from the cygutils-extra package). Jun 17, 2016 at 15:09
  • 8
    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" Nov 30, 2017 at 19:30
  • 2
    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, 2018 at 10:32
15

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"
1
  • 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, 2017 at 23:02
10

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
                                         
3
  • wow thanks, found this after 100 obsolete answers. surprised their api has changed so many times Oct 29, 2020 at 21:17
  • 1
    Nice answer. Catch 22: You need to restart tmux completely (using tmux kill-server) after making changes to ~/.tmux.conf.
    – PhoemueX
    Dec 3, 2020 at 9:49
  • Same here...glad to have finally found this. Tried many things, but this worked for me.
    – Levi
    Mar 13, 2021 at 20:09
9

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.

1
  • This works brilliantly for me. Thanks!
    – codematix
    Mar 20, 2022 at 13:49
6

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'"
5

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

2
  • 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, 2014 at 16:04
  • Just figured it out. I didn't understand how the "vi-copy" keybinding table worked.
    – Ben Davis
    May 20, 2014 at 19:28
2

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

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

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

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.

1

macOS Big Sur (2021) and Tmux v3.1c:

I can attest that the below succeeds. That is after trying many of the above unsuccessfully.

# Use vim keybindings in copy mode
setw -g mode-keys vi

# Setup 'v' to begin selection as in Vim
unbind -T copy-mode-vi 'v'
unbind -T copy-mode-vi 'y'
unbind -T copy-mode-vi MouseDragEnd1Pane
unbind -T copy-mode-vi Enter   #this is the default binding for copy (but not to system clipboard)

#(many tried, only this worked)
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 'pbcopy'
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel 'pbcopy'
bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel 'pbcopy'

You must log in to answer this question.

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