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).
Best Answer
Tmux 2.4+ with vi copy mode bindings and xclip:
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:
Gnome-terminal doesn't support setting the clipboard using xterm escape sequences so we should ensure the
set-clipboard
option is 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 forMouseDrag1Pane
) and pipes it to a shell command. This tmux command wascopy-pipe
before tmux 2.4, and has since changed tosend-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+:
Tmux 2.2 to 2.4:
Before tmux 2.2:
Copy after mouse drag support was originally added in Tmux 1.3 through setting the new
mode-mouse
option toon
. Tmux 2.1 changed the mouse support to the familiar mouse key bindings, but did not haveDragEnd
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 (theset-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 supportset-clipboard
, such as GNOME Terminal.