25

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.

My tmux dotfiles are available here (link).

48

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 -se c -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.

  • 3
    This high quality answer is what finally made it all click for me.. thanks! – cjauvin Mar 26 '18 at 20:01
  • 2
    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
  • None of the solutions in this thread work for me. – Yankee Aug 4 '19 at 4:56
  • 1
    Hi Josh, when I source-file ~/.tmux.conf, it turned out :Unknown key: MouseDragEnd1Pane – Xifeng Li 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 0:34
9

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
2

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

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.

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.