56

If I select text with a mouse in tmux in iTerm2 on macOS I get the selected text copied into my clipboard. I do not have to click any extra buttons - just select the text you want and you're done.

I've tested tmux in terminal.app on macOS but it doesn't work there - I have to hit y to copy the selection to my clipboard.

I thought that there is a mouse binding (something like MouseOnSelection similar to MouseDown1Pane) but I couldn't find anything useful on the web and man tmux.

I wonder if there is a way to have a similar behaviour on Ubuntu 16.10 - preferably in the Gnome terminal.

5 Answers5

90

Tmux 2.4+ with vi copy mode bindings and xclip:

set-option -g mouse on
set-option -s set-clipboard off
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -selection clipboard -i"

For older tmux versions, emacs copy mode bindings (the default), or non-X platforms (i.e., no xclip), see the explanation below.


Explanation: First we need to enable the mouse option so tmux will capture the mouse and let us bind mouse events:

set-option -g mouse on

Gnome-terminal doesn't support setting the clipboard using xterm escape sequences so we should ensure the set-clipboard option is off:

set-option -s set-clipboard off

This option might be supported and enabled by default on iTerm2 (see set-clipboard in the tmux manual), which would explain the behavior on there.

We can then bind the copy mode MouseDragEnd1Pane "key", i.e., when the first mouse button is released after clicking and dragging in a pane, to a tmux command which takes the current copy mode selection (made by the default binding for MouseDrag1Pane) and pipes it to a shell command. This tmux command was copy-pipe before tmux 2.4, and has since changed to send-keys -X copy-pipe[-and-cancel]. As for the shell command, we simply need something which will set the contents of the system clipboard to whatever is piped to it; xclip is used to do this in the following commands. Some equivalent replacements for "xclip -selection clipboard -i" below on non-X platforms are "wl-copy" (Wayland), "pbcopy" (macOS), "clip.exe" (Windows, WSL), and "cat /dev/clipboard" (Cygwin, MinGW).

Tmux 2.4+:

# For vi copy mode bindings
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -selection clipboard -i"
# For emacs copy mode bindings
bind-key -T copy-mode MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -selection clipboard -i"

Tmux 2.2 to 2.4:

# For vi copy mode bindings
bind-key -t vi-copy MouseDragEnd1Pane copy-pipe "xclip -selection clipboard -i"
# For emacs copy mode bindings
bind-key -t emacs-copy MouseDragEnd1Pane copy-pipe "xclip -selection clipboard -i"

Before tmux 2.2:

Copy after mouse drag support was originally added in Tmux 1.3 through setting the new mode-mouse option to on. Tmux 2.1 changed the mouse support to the familiar mouse key bindings, but did not have DragEnd bindings, which were introduced in 2.2. Thus, before 2.2 I believe the only method of setting the system clipboard on mouse drag was through the built-in use of xterm escape sequences (the set-clipboard option). This means that it's necessary to update to at least tmux 2.2 to obtain the drag-and-copy behavior for terminals that don't support set-clipboard, such as GNOME Terminal.

Josh Brobst
  • 1,465
  • 4
    This high quality answer is what finally made it all click for me.. thanks! – cjauvin Mar 26 '18 at 20:01
  • 3
    For anyone trying to do this on a mac because they've changed their terminal functionality and the default method as stated by the OP no longer works (I'm using Byobu which has its own copy buffer): replacing the xclip... statement with a simple pbcopy does the trick. – Darrel Holt Jul 15 '19 at 20:04
  • 1
    None of the solutions in this thread work for me. – Ed Peguillan III Aug 04 '19 at 04:56
  • 1
    Hi Josh, when I source-file ~/.tmux.conf, it turned out :Unknown key: MouseDragEnd1Pane – DennisLi Oct 14 '19 at 10:23
  • @XifengLi Which tmux version and OS are you using? I believe you need at least tmux 2.2 for the DragEnd keys. – Josh Brobst Dec 11 '19 at 00:34
  • this works. however, the selection contains a at the end, so when pasted, the terminal tries to execute the command, which can be annoying sometimes – Tropilio Feb 24 '20 at 16:18
  • worked on mint 20 for me after many other solutions ineffective including yank.tmux – zzapper Dec 15 '20 at 10:55
  • The original answer uses ""xclip -se c -i", but note that very recent versions of xclip added the "-secure" flag (commit a21e11baba), so "-se" no longer is an abbreviation for "-selection". It is better to spell out "-selection" as the parameter. – eakst7 May 05 '21 at 15:02
  • 1
    @eakst7 I'll update the answer; thanks for the information! I'm not sure why I abbreviated the selection flag in the first place, as I didn't do it in the full explanation. – Josh Brobst May 08 '21 at 18:49
  • 1
    As suggested by @DarrelHolt, I have to use pbcopy eg: bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "pbcopy" – IanVaughan Apr 22 '22 at 14:28
40

Yet another extension is about using Shift key.

Hold Shift while selecting text with mouse. Now you get a standard right-click menu (keep holding or press Shift again) and you can use Ctrl+Shift+C and Ctrl+Shift+V to copy and paste. Copied text will be also available in system clipboard.

Tested on Ubuntu 18.04.1 with tmux 2.6.

Source: https://forum.upcase.com/t/tmux-ctrl-shift-c-and-ctrl-shift-v-bindings/1208.

13

As an extension to the accepted answer, people often find that disturbing when releasing the mouse button exits the copy-mode (this is what happens with copy-pipe-and-cancel). See: https://github.com/tmux/tmux/issues/140 . Therefore, in newer tmux (I use 2.6) we can use copy-pipe together with clear selection to copy to selection but do not exit the copy-mode:

bind-key -T copy-mode-vi MouseDragEnd1Pane send -X copy-pipe "xclip -selection clipboard -i" \; send -X clear-selection
Gabor Marton
  • 231
  • 2
  • 5
3

Extending both answers to copy from msys2 to the Windows clipboard (and clear the selection):

# For vi copy mode bindings
bind -Tcopy-mode-vi MouseDragEnd1Pane send -X copy-pipe "cat > /dev/clipboard" \; send -X clear-selection
# For emacs copy mode bindings
bind -Tcopy-mode MouseDragEnd1Pane send -X copy-pipe "cat > /dev/clipboard" \; send -X clear-selection
  • Good point. I use copy-mode myself (emacs style) and so had pasted my own config after testing. I've updated the answer to include both as in the accepted answer. – Shawn Hoover Nov 30 '17 at 17:09
0

to add to above answers, this works for me on mac. Using xclipboard does not work in my case.

bind -T copy-mode-vi  MouseDragEnd1Pane   send-keys -X copy-pipe-and-cancel pbcopy