35

tmux select problem

My Problem

When I select text from tmux using the mouse, the block selection spans to neighbouring panes.

What Have I Tried

My Question

How can I configure tmux to allow mouse selection in multiple-pane mode?

Adam Matan
  • 2,593

6 Answers6

23

It depends on the version of tmux. When tmux mouse is on then the mouse selections will not span panes and will be copied into tmux's selection buffer. When tmux mouse is off (as it is in the description) then the mouse selection will be native X (and span panes).

I add the following to my ~/.tmux.conf. It will enable CTRL+b M (to turn tmux mouse on) and CTRL+b m (to turn tmux mouse off).

For tmux 1.x - 2.0

# Toggle mouse on
bind-key M \
  set-window-option -g mode-mouse on \;\
  set-option -g mouse-resize-pane on \;\
  set-option -g mouse-select-pane on \;\
  set-option -g mouse-select-window on \;\
  display-message 'Mouse: ON'

Toggle mouse off

bind-key m
set-window-option -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 ;
display-message 'Mouse: OFF'

For tmux 2.1+

# Toggle mouse on
bind-key M \
  set-option -g mouse on \;\
  display-message 'Mouse: ON'

Toggle mouse off

bind-key m
set-option -g mouse off ;
display-message 'Mouse: OFF'

Or, to use a single bind-key toggle for tmux 2.1+

# Toggle mouse on/off
bind-key m \                  
set-option -gF mouse "#{?mouse,off,on}" \;\
display-message "#{?mouse,Mouse: ON,Mouse: OFF}"

When tmux mouse is on, and a selection is made with the mouse, releasing the left mouse button should copy it to the tmux selection buffer and CTRL+b ] will paste it.

  • 1
    You can also use a single key mapping to toggle between mouse mode on and off by doing bind m set -gF mouse "#{?mouse,off,on}" (replace m with the key you want to use). – Raphael Schweikert Nov 04 '21 at 13:22
  • Brilliant @RaphaelSchweikert ... I never thought of using FORMATS. Thanks for sharing! – Joseph Tingiris Nov 04 '21 at 22:12
  • 1
    @JosephTingiris Thanks. I did feel quite brilliant when I figured this out. That is until I realized doing set-option -g mouse without any argument will already toggle the setting, no FORMATs needed. – Raphael Schweikert Nov 05 '21 at 07:21
  • is it possible to keep mouse selection on, but select to copy to system buffer so that I can ctrl+v to paste? – 3tbraden Jul 26 '22 at 05:32
  • @RaphaelSchweikert I just read what you wrote on 11/5 ... remember, remember ... I don't have time to try it now but that does look better. – Joseph Tingiris Jul 31 '22 at 12:20
  • @tbraden I use xsel with a tmux key binding to do similar. e.g. bind-key C-c run-shell "tmux show-buffer | xsel -b -i" is my CTRL+A+CTRL+C (I don't use the default bind key because it conflicts with n/vi/m & CTRL+A is like screen). anyway, when something's in my tmux selection buffer then i can dump it to the x clipboard (using xsel), thus enabling the regular CTRL+V to be able to paste it anywhere i want. lemonade and others work, but i think that ? is a bigger topic & separate so question. – Joseph Tingiris Jul 31 '22 at 12:35
16

I'm not sure about 2.7, I'm using tmux 3.1c. With 3.1c, you can press prefix+z to maximize ("zoom") the pane (where prefix is Ctrl+b by default). Then, you can do your copying, and prefix+z again to switch back.

Kusalananda
  • 333,661
Xin
  • 161
4

Usually it is expected behaviour while copying with button pressed - You are escaping the tmux to underlying terminal which does not care about the vertical boundary.

Otherwise it should highlight only what you select. Also selection usually disappears as soon as you release the mouse. But it does copy the selection to its internal buffers to be available for later pastes.

You may also find this answer to related question useful:

How to copy and paste with a mouse with tmux

Thava
  • 141
3

The following solution works for me with tmux 3.1c (tmux -V).

It needs the xclip command (e.g., apt install xclip).

Shut down any running tmux instance to make sure the upcoming edits to your tmux configuration file ~/.tmux.conf are being used:

tmux kill-server

Now edit file ~/.tmux.conf and have the following configurations in it:

set -g mouse on

for tmux's default "emacs" mode-keys (copy-mode)

bind-key -T copy-mode MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -selection primary -filter | xclip -selection clipboard"

if you are using "vi" mode-keys (copy-mode-vi), it will be probably like so:

set-window-option -g mode-keys vi

bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -selection primary -filter | xclip -selection clipboard"

From now on, in tmux, left-mouse-button-selecting-without-holding-shift should

  • as usual select text within a pane (selected text being colored in orange/yellow)
  • and as usual, after releasing the left mouse button, the selection is copied into tmux's own "buffer" (pastable with Ctrl + b then ])
  • but now new, after releasing the left mouse button, the selection is automatically put into both the "primary" and "clipboard" clipboard (I never understood this clipboard distinction; it only causes gotchas, so I just want my selection be dumped to any clipboard that exists).

If this setup still does not work, make sure the tmux environment is configured correctly:

  • Output from tmux list-keys | grep MouseDragEnd1Pane shall include the configured bind-key.
  • Check the active tmux set-clipboard configuration with tmux show -s | grep clip. Mine is set-clipboard external, the tmux default, as of this writing, since tmux 2.6.
  • Make sure to tmux kill-server and start tmux anew to have any configuration changes be used.
Abdull
  • 853
1

@Raphael Schweikert mentioned in a comment that just setting set-option -g mouse fixes it (rather than set -g mouse on), which worked for me with nothing else needed.

0

The "correct" or "complete" answer some of the above, and at the same time none of the above :) This is the logic:

The logic

In Tmux you can activate mouse by adding either of set -g mouse on or set-option -g mouse (add it to your tmux config file ~/.tmux.conf). This far, have bee explained by others as well.

Now, here is the deal, if you hold Shift and select the text, and then Ctrlc, it will copy it to the same machine that you have the terminal in. This does not respect the tmux splits as it is your terminal that is handling the selection. If you select the text without holding Shift, then you are using tmux to select the text for you (alternatively you can manually select the text (read the end of this post)). This way the selection would respect the panes and splits, but it will copy the selection in the clipboard of the machine that is running tmux.

In other words, if you use computer1 SSH into a computer2 and run tmux there, holding Shift will not respect tmux panes but copies the text in the clipboard of computer1. But if you don't hold Shift, the selection respects tmux panes but the selection goes to clipboard of computer2.

So to save yourselft some headache, the easiest way is to "zoom" the pane you are interested in (Ctrlb then z), hold shift to select the text, copy (usually CtrlShiftc) and then toggle back the zoom (Ctrlb then z).

Keyboard-driven selection and copy

  1. Enter selction mode by Ctrlb then [
  2. use the keybinding you set by the status-keys (e.g mine is set -g status-keys emacs which means I use Emacs bindings: Ctrln for down, Ctrlp for up, CtrlSpace to start selection, and so on)
  3. when selection is done, press your set key to copy (in my case in Emacs mode it Altw)
  4. to paste use Ctrlb then ]