tmux: Select and copy pane text with mouse

21,620

Solution 1

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.

Solution 2

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.

Solution 3

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

Share:
21,620

Related videos on Youtube

Adam Matan
Author by

Adam Matan

Team leader, developer, and public speaker. I build end-to-end apps using modern cloud infrastructure, especially serverless tools. My current position is R&D Manager at Corvid by Wix.com, a serverless platform for rapid web app generation. My CV and contact details are available on my Github README.

Updated on September 18, 2022

Comments

  • Adam Matan
    Adam Matan over 1 year

    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?

    • VocalFan
      VocalFan over 5 years
      Which tmux version are you using, and which terminal emulator?
    • Adam Matan
      Adam Matan over 5 years
      tmux 2.7, iterm2 3.2.0
  • evaristegd
    evaristegd about 4 years
    That's a great link, thank you!
  • Stabledog
    Stabledog almost 4 years
    Simple and efficient and does what I need.
  • Xin
    Xin over 3 years
    @Kusalananda Thank you for making the answer more clear
  • avocado
    avocado over 2 years
    Why it's so complicated to do this in tmux, sad
  • Raphael Schweikert
    Raphael Schweikert over 2 years
    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).
  • Joseph Tingiris
    Joseph Tingiris over 2 years
    Brilliant @RaphaelSchweikert ... I never thought of using FORMATS. Thanks for sharing!