Getting back old copy paste behaviour in tmux, with mouse

TerminalGnu ScreenTmux

Terminal Problem Overview


This is what I used to do in tmux to copy-paste (using the mouse, the keyboard works differently and it is not what I am interested about):

  1. Select text with mouse, left-button pressed
  2. Paste text with middle-button

I have upgraded my OS, and this has gotten a new tmux version. I have not changed my .tmux.conf config file.

This is what I have to do with the current version of tmux, 1.6 (which comes pre-packaged in the latest crunchbang linux):

  1. Select text with mouse, left-button pressed and shift key
  2. Paste text with middle-button
  3. Terminal gets blocked, a litte information area shows some numbers on the top right of the current pane (i.e. [0/24], probably something related to how many characters have been pasted), which mean little to me and I do not need / want (edit: it seems copy-mode is entered automatically here)
  4. I have to press the q key to get a functional terminal again.

This is too much hassle for something I do dozens of times a day. How to get the old mechanism working again?

Terminal Solutions


Solution 1 - Terminal

  1. Copy the text: select the text and press mouse left-button with shift key press too.
  2. Paste the text with shift key + middle-button

Solution 2 - Terminal

To restore the default copy/paste configuration you need to (at least temporarily) turn off mouse support within tmux:

prefix : set -g mouse off

Where prefix is the tmux access key (Ctrl+B by default unless you re-map it). : starts command mode and set -g sets the parameter globally.

When mouse mode is turned off, the standard copy/paste functions provided by your operating system work as expected.

Something else you might want to do is 'maximise' the current pane, so you can copy multiple lines easily.


If you’re working with an old (pre-2.1) version of tmux, you instead need to use the following:

prefix : set -g mode-mouse off

There are more details and some handy key bindings to automate all this here:

http://tangledhelix.com/blog/2012/07/16/tmux-and-mouse-mode/

The main thrust of the article linked to above is this excerpt from .tmux.conf:

# disable mouse control by default - change 'off' to 'on' to enable by default.
setw -g mode-mouse off
set-option -g mouse-resize-pane off
set-option -g mouse-select-pane off
set-option -g mouse-select-window off
# toggle mouse mode to allow mouse copy/paste
# set mouse on with prefix m
bind m \
    set -g mode-mouse on \;\
    set -g mouse-resize-pane on \;\
    set -g mouse-select-pane on \;\
    set -g mouse-select-window on \;\
    display 'Mouse: ON'
# set mouse off with prefix M
bind M \
    set -g mode-mouse off \;\
    set -g mouse-resize-pane off \;\
    set -g mouse-select-pane off \;\
    set -g mouse-select-window off \;\
    display 'Mouse: OFF'
# zoom this pane to full screen
bind + \
    new-window -d -n tmux-zoom 'clear && echo TMUX ZOOM && read' \;\
    swap-pane -s tmux-zoom.0 \;\
    select-window -t tmux-zoom
# restore this pane
bind - \
    last-window \;\
    swap-pane -s tmux-zoom.0 \;\
    kill-window -t tmux-zoom

Solution 3 - Terminal

If "set -g mode-mouse on" you can do this trick:

On Mac, press "fn" button, then select text and copy with mouse right click or keyboard cmd+c.

Solution 4 - Terminal

tmux 2.6+

Mouse needs to be activated for this to work, so do: Ctrl + B and then type :set -g mouse on. (Or better: set this setting in your tmux.conf for consistency.)

Windows 10

With mouse mode activated, on Windows you need to press Shift as explained below.

Copy
  1. Hold down Shift and select with your mouse the text you want to copy.
  2. Now right click to copy the selected text (without holding Shift).

Edge Case: Horizontally Splitted Panes

When working with horizontally splitted panes the selecting part is not that easy because a selection over multiple lines also spans over multiple panes, selecting text parts you don't want to select. To avoid the selection to leave the current pane also press Ctrl while pressing Shift (thanks to @Franck).

Another workaround would be to quickly change the layout of the panes (e.g. with Ctrl + B and then Space) and then change it back afterwards.

Paste
  1. Hold down Shift and right click to insert the copied text.

Simple as that. Enjoy!

macOS

With mouse mode activated, on macOS you need to press fn instead. To copy the selection use CMD + C as usual.

Solution 5 - Terminal

Modified from here - I use xclip instead of xsel in the original:

bind -T root MouseDown2Pane run -b "xclip -o | tmux load-buffer - && tmux paste-buffer -s ' '"

This is working merrily for me in tmux 2.5-rc2

Solution 6 - Terminal

Use <prefix>+m toggle mouse mode on or off

bind m run "if [[ `tmux show-option -w | grep mode-mouse.*on` ]]; then toggle=off; else toggle=on; fi; tmux display-message \"mouse tmux: \$toggle\"; tmux set-option -w mode-mouse \$toggle &> /dev/null; for cmd in mouse-select-pane mouse-resize-pane mouse-select-window; do tmux set-option -g \$cmd \$toggle &> /dev/null; done;"

Solution 7 - Terminal

I had problems getting Christian's example to work for Tmux 2, I think some typos. I got the below to work and is a bit easier to read and sets both global and window mode. hth someone. new user and tmux is great!

bind m run "\
    tmux show-options -g | grep -q "mouse\\s*on"; \
    if [ \$? = 0 ]; \
    then  \
        toggle=off;  \
    else  \
        toggle=on;  \
    fi;  \
    tmux display-message \"mouse is now: \$toggle\";  \
    tmux set-option -w mouse \$toggle; \
    tmux set-option -g mouse \$toggle; \
    "

Solution 8 - Terminal

For users of Mac + iTerm2 + tmux(version >2.1):

Ensure the mouse mode is set in tmux config (Just add set -g mode-mouse on in ~/.tmux.conf). Now, to copy the text inside a pane:

  1. Press option + command and select the text you wanna copy using the mouse cursor. It's like cropping a pic.
  2. The selected text would have copied automatically (no need of command + c). Just paste it by usual means.

Solution 9 - Terminal

This is a modified version of Kaixuan's answer that is compatible with Tmux 2.1.

`bind m run "if [[ `tmux show-options -w | grep mouse.*on` ]]; then toggle=off; else toggle=on; fi; tmux display-message \"mouse tmux: \$toggle\"; tmux set-option -w mouse \$toggle &> /dev/null;`"

All the mode-mouse options have been combined into one mouse option and show-option had to be replaced with show-options

Solution 10 - Terminal

in ~/.tmux.conf:

set -g mouse off

having bind r source-file ~/.tmux.conf may be useful too so you can do ctrl-d r to reload the config for instance.

Solution 11 - Terminal

Based on the other answers posted here, I have created a concise solution that works with (at least) tmux 2.8 and tmux 3.1.

You can toggle tmux mouse support on and off, by pressing prefix-M. In the .tmux.conf file, include this line:

bind-key -T prefix m run "m=$(tmux show -g mou|grep -q on;echo $?);tmux set -g mou $m;tmux display mouse:\$m"

This will display mouse: 1 when enabled and mouse: 0 when disabled. Repeatedly pressing prefix-M will toggle the mouse mode between on and off.

See also: tmux mouse support

EDIT: If you see a command returned 1 error message then you will need to use the following instead for tmux 3.1 or tmux 3.2:

bind-key -T prefix m set -g mouse\; display 'Mouse: #{?mouse,ON,OFF}'

(this solution was previously mentioned)

Solution 12 - Terminal

Here is an updated version of the mouse toggle, tested on tmux v3.1

Since the code is a one-liner, in order to keep it short I just used t for the variable name, and I replaced the if statement with a conditional looking for the letter n. The conditional allows us to cut a lot of spaces and semi-colons out of it, further shortening the line.

bind m run "[[ `tmux show -gv mou` = *n* ]]&&t=off||t=on;tmux set -g mou \$t;tmux display-message \"mouse mode: \$t\""

Here is the code snippet expanded into a more readable form

[[ `tmux show -gv mouse` = *n* ]] && t=off || t=on
tmux set -g mouse $t
tmux display-message "mouse mode: $t"

Solution 13 - Terminal

I use following binding to select text with mouse left button pressed -

bind -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "/mnt/c/Windows/System32/clip.exe"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionblueFastView Question on Stackoverflow
Solution 1 - TerminalYves BlusseauView Answer on Stackoverflow
Solution 2 - Terminaldr-janView Answer on Stackoverflow
Solution 3 - TerminalfluderView Answer on Stackoverflow
Solution 4 - TerminalwinklerrrView Answer on Stackoverflow
Solution 5 - TerminalAndyView Answer on Stackoverflow
Solution 6 - TerminalKaixuan WangView Answer on Stackoverflow
Solution 7 - TerminalGoblinhackView Answer on Stackoverflow
Solution 8 - TerminalUmashankarView Answer on Stackoverflow
Solution 9 - TerminalChristian SchlenskerView Answer on Stackoverflow
Solution 10 - TerminallocalhostdotdevView Answer on Stackoverflow
Solution 11 - TerminaljftugaView Answer on Stackoverflow
Solution 12 - TerminalJacob LundqvistView Answer on Stackoverflow
Solution 13 - TerminalwebhView Answer on Stackoverflow