mrt181 mrt181 - 8 months ago 207
Bash Question

How to write if statement in .tmux.conf to set different options for different tmux versions?

I have a .tmux.conf which I use on different machines with different tmux versions installed.

I want to set different mouse options, depending on the tmux version.
On one machine I have version

on the other

I do not get his part right

if "[[(( $(tmux -V | cut -c 6-) < 2.1 ))]]" \
"set -g mode-mouse on;" \
"set -g mouse-resize-pane on;" \
"set -g select-pane on;" \
"set -g select-window on" "set -g mouse on"

When I source the file

$ tmux source-file .tmux.conf

I get this message

.tmux.conf:12: unknown command: set -g mouse-resize-pane on

The machine where I run it has version
so it shouldn't set the four options.

I want to set the four options when running tmux 2.0 or less or the one option when running tmux 2.1.

This bash statement works

$ tmux -V
tmux 2.1
$ if [[(( $(tmux -V | cut -c 6-) < 2.1 ))]];then echo $?;else echo $?;fi


Tmux's if-shell will only execute following commands if the previous command completed successfully meaning it will only proceed if the error code equals 0. From the man page:

Execute the first command if shell-command returns success or the second command otherwise.

So using bash syntax we can use

[[ `tmux -V | cut -d' ' -f2` -lt 2.1 ]]

to check whether or not the tmux version is greater than or equal to 2.1. Tmux's if-shell, again, will only execute your mouse commands if the previous command executed without an error code so using false we can prevent tmux from executing the mouse commands:

if-shell "if [[ `tmux -V | cut -d' ' -f2` -lt 2.1 ]]; then true; else false; fi" \
    'set -g mode-mouse on; set -g mouse-resize-pane on; set -g mouse-select-pane on; set -g mouse-select-window on'

You can then use this to set it for later versions of tmux if you want:

if-shell "if [[ `tmux -V | cut -d' ' -f2` -ge 2.1 ]]; then true; else false; fi" \
    'set -g mouse on; set -g mouse-utf8 on'