96

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?

Naftuli Kay
  • 39,676

10 Answers10

152

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.

ndemou
  • 2,809
Ben Davis
  • 2,139
  • 1
    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
  • 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) – RubenLaguna May 28 '15 at 11:26
  • 6
    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
  • 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" – Matthias Braun Nov 30 '17 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 '18 at 10:32
  • Just saw SnoringFrog's windows technique now. Prior to knowing that, I got it working for tmux <2.5 in cygwin/windows with:

    bind-key -t vi-copy y copy-pipe "cat >/dev/clipboard"

    – Gurce Jan 08 '19 at 03:10
  • Matthias Braun's solution did not work for me initially, but after I made sure to kill all tmux sessions using tmux ls and tmux kill-session -t [id] it started working. You can enter tmux command mode by doing prefix + : and there you can enter the bind -T copy-mode-vi ... command manually to see if it'll work on a fresh session at all – walnut_salami Jan 11 '20 at 12:07
  • -bash: bind: -T: invalid option – AjaxLeung Jan 26 '20 at 09:10
  • Ah, you should have mentioned that this bind line isn't a terminal command. It belongs in the ~/.tmux.conf file – AjaxLeung Jan 26 '20 at 09:14
  • This is all nice and clear, but does not explain how to achieve one the OP's goals: remap in this setup <Space> (for starting visual mode/selection) to actual v button(to stay consistent with vim style). Or is it simply impossible? – Drew Jan 07 '21 at 12:10
  • When used over ssh, this solution will copy to remote X clipboard (I presume) rather than the local X clipboard, which would be the intended effect. To copy it to local clipboard, the copy-pipe would have to call a command that relays selection contents over ssh and executes a remote command similar to the one above (xclip) – axolotl Oct 05 '21 at 14:38
  • It is something I would love to be able to do, but sounds like a lot of work for now. I will resort to cat > ~/.cache/clipboard as one comment above suggests (but with /dev/clipboard) – axolotl Oct 05 '21 at 14:39
  • For those using 3.3a, and with xclip installed, a simple set -s copy-command 'xclip -selection clipboard' is sufficient. Note this does not bind with Y, you don't need press y after enter. See https://github.com/tmux/tmux/wiki/Clipboard#available-tools – unifreak Jan 13 '23 at 22:46
  • If you additionally to what the config mentioned in this answer provides want to use enter to commit the selection to clipboard, add unbind enter before bind -T copy-mode-vi enter send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard' – Wu Wei Jan 27 '23 at 23:34
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"
jruz
  • 251
  • 2
  • 2
11

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

Mohammed
  • 211
  • wow thanks, found this after 100 obsolete answers. surprised their api has changed so many times – Jeff Puckett Oct 29 '20 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 03 '20 at 09:49
  • Same here...glad to have finally found this. Tried many things, but this worked for me. – Levi Mar 13 '21 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.

7

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'"
Felix
  • 99
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.)

Chris Johnsen
  • 20,101
  • 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 -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
2

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

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

  • 2
    macOS users, for tmux >= 2.5: bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'pbcopy' – dz902 Oct 24 '19 at 03: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'

0

I replaced my X11 clipboard integration (xsel), Vim style (copy with y, paste with p)

bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xsel -i -p && xsel -o -p | xsel -i -b"            
bind-key p run "xsel -o | tmux load-buffer - ; tmux paste-buffer"

with Wayland clipboard integration (wl-clipboard)

bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "wl-copy"                                          
bind-key p run "wl-paste --no-newline | tmux load-buffer - ; tmux paste-buffer"
noraj
  • 340