Jay LaCroix
Jay LaCroix

Reputation: 171

Enabling mouse support across different tmux versions

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?

Upvotes: 15

Views: 11261

Answers (3)

Choffee
Choffee

Reputation: 81

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

Upvotes: 6

jayeff
jayeff

Reputation: 1787

Building on the answer of @douglas-su I found a solution which currently works (see caveat below).

Follow step 1 + 2 of his answer: create two files with options for < 2.1 and >= 2.1 options. Instead of step 3 insert the following snippet in your .tmux.conf:

if-shell "[[ `tmux -V | cut -d ' ' -f2 | sed 's/[a\.]//g'` -ge 21 ]]" 'source ~/.tmux/tmux_ge_21.conf' 'source ~/.tmux/tmux_lt_21.conf'

Explanation:

  • cut -d ' ' -f2 selects the second part of tmux -v. Example: returns '2.1' for 'tmux 2.1'
  • sed 's/[a\.]//g' replaces all dots . and a's in the version string. Example: returns 19 for '1.9a'

Caveat: This solution probably does not work for all eternity but should work fine for all releases of tmux so far (current version is 2.1). If for any reason a updated older version would be released (e.g. 2.0.1 for a security fix or something similar) the proposed solution would no longer work as 201 >= 21.

Hope this helps.

Upvotes: 0

Douglas Su
Douglas Su

Reputation: 3354

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.

Upvotes: 1

Related Questions