18

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).

34

Tmux 2.4+:

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"

Before tmux 2.4:

set-option -g mouse on
set-option -s set-clipboard off
bind-key -t vi-copy MouseDragEnd1Pane copy-pipe "xclip -se c -i"

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.

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"

Before tmux 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"
  • 2
    This high quality answer is what finally made it all click for me.. thanks! – cjauvin Mar 26 '18 at 20:01
7

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

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.