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 2.0
on the other 2.1
.
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 2.1
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
1
Tmux's
if-shell
can be used to check the ZSH version.checks whether or not the tmux version is greater than or equal to 2.1. Using this we can set your mouse commands depending on the ZSH version.
And set it for later versions of tmux:
This is kind of a hastle. The correct way to do this within tmux (not relying on an external shell script) combines features of both Vincent and jdloft's responses.
The
if-shell
command in tmux is used asNote that tmux shell-command expansion will expand variables of the form
#{pane_current_path}
but otherwise will leave the command alone.More importantly, note that tmux uses
/bin/sh -c
to execute the shell command we specify. Thus, the command must be POSIX compliant, so tests of the form[[
are not guaranteed to be portable. Modern Ubuntu and Debian systems, for example, symlink/bin/sh
todash
.We want to run a POSIX compliant shell command that tests the tmux version and returns 0 (true) if the desired version is found.
Example:
This correctly deals with the fact that we are doing floating point arithmetic, so
bc
is required. Additionally, there is no need for an if/then/else/fi construct, as the[
operator produces a truthy value by itself.A couple notes
;
/bin/sh -c
. Other approaches that use[[
or other non-POSIX syntax are not guaranteed to work.EDIT: A previous version of this answer used
[[
, which doesn't work on systems that don't use bash. Replacing with[
solves this.Based on @ericx's answer and @thiagowfx's answer I put the following together which covers the listed incompatibilties from version 2.0 onwards:
if-shell
doesn't always work. Instead, I use a shell script for loading the correct version of tmux.conf:In .tmux.conf:
In verify_tmux_version.sh:
For more details: https://gist.github.com/vincenthsu/6847a8f2a94e61735034e65d17ca0d66
On some machines I was getting a false-positive result with the double bracket ('[[') syntax. So I came up with an alternative using awk:
I also stumbled over configuration mismatches in different tmux versions. After reviewing all the solutions here and in this related question on SuperUser, I've implemented the following variant:
With this, version-specific configuration can be put in (multiple) configuration snippets for a particular version. This is similar to the solution of @VincentHsu, but:
~/.tmux.conf
. Other solutions like the one from @TomHale duplicate the version test for each configuration element.