Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling mouse support across different tmux versions

Tags:

tmux

I manage several Linux machines, some with tmux version 2.1 in the repositories, and others with tmux versions less than 2.1. I use mouse mode, and I understand that in tmux 2.1, the option to enable mouse mode has changed to:

set -g mouse on

Since I use different distributions each with a different version of tmux, I wanted to make one .tmux.conf file that would enable the appropriate mouse option depending on the version.

So, I added the following to my .tmux.conf:

# Mouse Mode
if-shell "[[ `tmux -V |cut -d ' ' -f2` -ge 2.1 ]]" 'set -g mouse on'
if-shell "[[ `tmux -V |cut -d ' ' -f2` -lt 2.0 ]]" 'set -g mode-mouse on'
if-shell "[[ `tmux -V |cut -d ' ' -f2` -lt 2.0 ]]" 'set -g mouse-resize-pane on'
if-shell "[[ `tmux -V |cut -d ' ' -f2` -lt 2.0 ]]" 'set -g mouse-select-pane on'
if-shell "[[ `tmux -V |cut -d ' ' -f2` -lt 2.0 ]]" 'set -g mouse-select-window on'

Unfortunately, this does not work. tmux doesn't show any errors, but it also doesn't enable mouse mode either.

Is there some error in my logic which is preventing this configuration from working?

like image 629
Jay LaCroix Avatar asked Dec 09 '15 19:12

Jay LaCroix


People also ask

How do I enable mouse mode on Tmux?

Configure Tmux for Mouse SupportRestart Tmux or run the Tmux command source ~/. tmux. conf , and you should be able to scroll up or down the scrollback with your mouse. You'll also be able to select a pane with the mouse and resize them.

How do I get my mouse to scroll in Tmux?

Press the Ctrl + B keys, then the subsequent “[]” key. To navigate the Tmux interface, use the standard keyboard navigation keys such as up/down arrows, page up/page downpage up/page downThe Page Up and Page Down keys are two computer keys that you press to either scroll up or scroll down on documents. The page up and page down scrolling distance sometimes varies depending on which documents you are reading.https://simple.wikipedia.org › Page_Up_and_Page_Down_keysPage Up and Page Down keys - Simple English Wikipedia, the free ..., etc. When a user has finished scrolling, he can exit Tmux's scroll mode by pressing the Q key.

How do I scroll up TMU?

Once you're in copy mode, you can continue pressing M-Up to scroll 1 line up. The usual PageDown and PageUp controls are available to scroll by whole screen, and extra M-PageDown and M-PageUp to scroll by a half of screen (really convenient).


2 Answers

Building on the last two answers but replacing the shell command as below. Add this to the main config:

if-shell "tmux -V |awk ' {split($2, ver, \".\"); if (ver[1] < 2) exit 1 ; else if (ver[1] == 2 && ver[2] < 1) exit 1 }' " 'source .tmux/gt_2.0.conf' 'source .tmux/lt_2.1.conf'

This uses awk to split the version number, a clearer version of this code is:

split($2, ver, ".")  #Split the second param and store it in the ver array
if ver[1] < 2) # if it's less than v2.0
   exit 1
else
   if (ver[1] == 2) # if it's version 2.n look at next number
       if (ver[2] < 1) # If the second number is less than 1 (2.1)
          exit 1
# else we exit 0

Then split the config out into the two config files.

lt_2.1.conf contains

set -g mode-mouse on
set -g mouse-resize-pane on
set -g mouse-select-pane on
set -g mouse-select-window on

gt_2.1.conf contains

set -g mouse-utf8 on
set -g mouse on
like image 156
Choffee Avatar answered Oct 04 '22 22:10

Choffee


It seems that set is not a command of tmux and you can't execute it in if-shell.

I have an alternative scheme:

  1. create two config files somewhere. Here we suppose that these two config files are tmux_ge_21.conf and tmux_lt_21.conf, they all locates at ~/.tmux/ directory.

  2. Fill contents below to these two files:

For tmux_ge_21.conf:

set -g mouse-utf8 on
set -g mouse on

For tmux_lt_21.conf:

set -g mode-mouse on
set -g mouse-resize-pane on
set -g mouse-select-pane on
set -g mouse-select-window on
  1. add the line below in your .tmux.conf:

    if-shell "[[ `tmux -V | awk '{print ($2 >= 2.1)}'` -eq 1 ]]" 'source ~/.tmux/tmux_ge_21.conf' 'source ~/.tmux/tmux_lt_21.conf'
    
  2. Execute tmux source ~/.tmux.conf in your terminal.


BTW: For tmux which greater than 2.1, the default act of mouse scroll is changed. If you want it act as before, you have to install this tmux plug: https://github.com/nhdaly/tmux-scroll-copy-mode

If you use this plugin, append set -g @plugin 'nhdaly/tmux-scroll-copy-mode' to tmux_ge_21.conf.


BTW2: -ge in [[ `tmux -V |cut -d ' ' -f2` -ge 2.1 ]] seems work only when compare two integer number, I am not very sure.

like image 36
Douglas Su Avatar answered Oct 04 '22 22:10

Douglas Su