55

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.

1

5 Answers 5

88

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.

10
  • 4
    This high quality answer is what finally made it all click for me.. thanks!
    – cjauvin
    Mar 26, 2018 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. Jul 15, 2019 at 20:04
  • 1
    None of the solutions in this thread work for me.
    – Yankee
    Aug 4, 2019 at 4:56
  • 1
    Hi Josh, when I source-file ~/.tmux.conf, it turned out :Unknown key: MouseDragEnd1Pane
    – DennisLi
    Oct 14, 2019 at 10:23
  • 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. May 8, 2021 at 18:49
37

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.

5
  • 2
    This is the simplest and it works like a charm. Exactly what I've been missing. Mar 19, 2020 at 1:25
  • 1
    For me as well this is the best solution
    – etuardu
    Jul 26, 2020 at 16:14
  • 2
    This saved me!!
    – urban
    Sep 25, 2020 at 14:21
  • this answer would be much cooler if it'd could work in remote / nested tmux sessions
    – ipatch
    Jun 1 at 20:22
  • This also solves the issue for me using Alacritty terminal on MacOS Dec 4 at 19:02
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
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
1
  • 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. Nov 30, 2017 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .