141

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?

blueFast
  • 41,341
  • 63
  • 198
  • 344

13 Answers13

304
  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
Yves Blusseau
  • 4,244
  • 4
  • 16
  • 7
  • 31
    For OSX users it's the option key, as described in this blog post: http://awhan.wordpress.com/2012/04/18/tmux-copy-paste-with-mouse/ – pangratz Aug 04 '14 at 14:09
  • 3
    In fact, after selecting text with Shift+LMB you can use OS functionality (Ctrl-Shift-C in my case) to copy to the non-X11 clipboard. – Eugene Pankov Feb 28 '15 at 08:13
  • 10
    The OP clearly states that they want to be able to copy and paste with the mouse, without using the keyboard. Pressing `Shift` (or option key on OSX) is the oldest trick in the book and works on most emulators, but doesn't answer the original question. – Lqueryvg Dec 04 '16 at 22:59
  • 2
    @pangratz Using the option key may work for iTerm users, but not for users of native Terminal. – anishpatel Jan 25 '19 at 21:27
  • 1
    Thank you for this workaround! It’s still suboptimal though – mouse wheel = scrolling the history, and all other mouse functions = normal would be much better. – Torsten Bronger Jun 10 '20 at 12:22
  • This does not work anymore in xterm-361 , see https://unix.stackexchange.com/questions/617021/how-do-i-restore-shiftmousebutton-select-when-mouse-protocol-is-enabled-in-xt – simon Nov 02 '20 at 10:09
101

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. Ctrl+B, z toggles the 'zoom' of the current pane.

The code below can be added to your .tmux.conf file to bind the m and M keys to 'mouse on' and 'mouse off' respectively. This lets you use Ctrl+B, m to turn on mouse support; Ctrl+B, M to turn it off.

# toggle mouse mode to allow mouse copy/paste
# set mouse on with prefix m
bind m \
    set -g mouse on \;\
    display 'Mouse: ON'
# set mouse off with prefix M
bind M \
    set -g mouse off \;\
    display 'Mouse: OFF'                                                                       

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
dr-jan
  • 2,124
  • 2
  • 21
  • 22
  • 17
    If you have tmux 1.8 or newer, there is a built-in command, Control-B z which will toggle the zoomed status of the current pane. So C-B z to zoom this pane to full screen, the same command again to restore. This removes the need to define the C-B + and C-B - key sequences at the end of the configuration above. – dr-jan Sep 17 '15 at 15:36
  • 9
    If you have tmux 2.1 or newer, all the mouse related commands have been made obsolete, replaced by 'mouse' which sets everything. So, to disable mouse mode by default use 'setw -g mouse off'. Remove any references to 'mode-mouse', 'mouse-resize-pane', 'mouse-select-pane' or 'mouse-select-window'. – dr-jan Mar 17 '16 at 14:09
  • 74
    However, mouse-mode on has its benefits: it lets me scroll. Is there a combination of settings that lets me use my terminal like a normal darn terminal? Copy/paste and scroll included? – Mihai Danila Aug 12 '16 at 22:44
  • I've not got this working yet, but see https://bbs.archlinux.org/viewtopic.php?id=204091 (which references https://github.com/tmux/tmux/issues/145) for details of configuring the mouse wheel to scroll up in copy/paste mode. – dr-jan Aug 24 '16 at 17:24
  • 1
    Is there some other setting I may have messed with, because on `OSx (Sierra)`, `option` doesn't help. Turning `mode-mouse off` allows me to select like normal, but then I can't copy into the clipboard (`cmd+C` or `cmd+shift+C` do nothing, or ring an error bell, or bring up the color-selector) – dwanderson Jan 17 '17 at 20:08
  • 5
    Tested with tmux v3, one binding to turn it on and off: `bind-key -T prefix m set -g mouse\; display 'Mouse: #{?mouse,ON,OFF}'` – Evgeny Mar 03 '20 at 16:57
  • Why are you using `setw` instead of `set`? – winklerrr Jan 27 '21 at 06:27
  • Why are you setting 4 different options for the mouse when according to this [documentation](https://man7.org/linux/man-pages/man1/tmux.1.html#MOUSE_SUPPORT) there is only one option `mouse` which enables all mouse features? – winklerrr Jan 27 '21 at 06:34
  • @winklerrr, as explained in the answer itself, modern tmuxes have a single mouse command `mouse off`, whilst versions before 2.1 use `mode-mouse off` and even older versions use the four mouse commands shown. So it all depends on which version of tmux you are using. – dr-jan Jan 31 '21 at 17:18
45

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.

fluder
  • 551
  • 5
  • 7
31

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.

winklerrr
  • 13,026
  • 8
  • 71
  • 88
11

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

Andy
  • 17,423
  • 9
  • 52
  • 69
10

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;"
Kaixuan Wang
  • 117
  • 1
  • 3
  • This worked perfect for me. Thanks so much. I wanted to be able to scroll in mouse mode, but that was breaking my ability to copy text. This allows for quick switching if you want to disable mouse mode. – Bijan Sep 18 '15 at 19:12
  • 10
    I use `bind m set-option mouse\; display-message "Mouse is now #{?mouse,on,off}"` – Maxim Suslov Aug 06 '18 at 10:51
  • user wants to be able to copy and paste, not yet more .tmux.conf customization! – Jack Wasey Dec 04 '18 at 15:49
7

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.
Umashankar
  • 71
  • 1
  • 5
6

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; \
    "
Goblinhack
  • 2,859
  • 1
  • 26
  • 26
  • 1
    Works for me. Tested against tmux HEAD from github (as of yesterday). – Lqueryvg Dec 04 '16 at 23:15
  • 2
    To make this work I had to change the `show-options` line to `tmux show-options -g | grep -q "mouse\\s*on"; \ `. This is because the regex `mouse.*on` matches `@scroll-in-moused-over-pane "on"` which is part of the plugin *nhdaly/tmux-better-mouse-mode* – Mandy Schoep Oct 01 '17 at 09:40
  • 1
    Simpler binding that does basically the same thing - `bind-key -T prefix m set -g mouse\; display 'Mouse: #{?mouse,ON,OFF}'` – Evgeny Mar 03 '20 at 17:00
3

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

Community
  • 1
  • 1
Christian Schlensker
  • 21,708
  • 19
  • 73
  • 121
0

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.

localhostdotdev
  • 1,795
  • 16
  • 21
0

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)

jftuga
  • 1,913
  • 5
  • 26
  • 49
0

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"
-1

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"
webh
  • 329
  • 4
  • 6