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.