Overview
Tmux changed the bind
syntax starting in version 2.4
. The syntax
changes are simple enough to make, however, some linux distros haven’t upgraded
to a newer version of tmux. For example, RHEL 7.4 still has tmux 1.8
in its
repositories by default while FreeBSD 11.1 has tmux 2.6
. To ensure maximium
backward compatibility with tmux viersions <2.4
the .tmux.conf
should check
the system’s version of tmux and act accordingly.
Config
First in the .tmux.conf
lets have tmux set its version in an environment
variable that will be referenced later:
run-shell "tmux setenv -g TMUX_VERSION $(tmux -V | cut -d' ' -f2)"
To set version specific bind
options for vi mode append the following to
.tmux.conf
. The conditional will use bind-key -T copy-mode-vi
for tmux
versions >=2.4
and bind -t vi-copy
for versions <2.4
:
# vi Mode
set -g mode-keys vi
# Add some vi comfort
unbind p
bind p paste-buffer
# Check version due to bind changes in 2.4. bc return true as 1
if-shell '[ $(echo "$TMUX_VERSION >= 2.4" | bc -l) == 1 ]' \
"\
bind-key -T copy-mode-vi v send -X begin-selection; \
bind-key -T copy-mode-vi y send -X copy-selection; \
bind-key -T copy-mode-vi V send -X rectangle-toggle; \
" "\
bind -t vi-copy v begin-selection; \
bind -t vi-copy y copy-selection; \
bind -t vi-copy V rectangle-toggle; \
"
Save .tmux.conf
after making the above modifications and relaunch tmux.
To validate these changes on tmux >=2.4
run the following from the tmux
command prompt(.i.e "command seq + :"
inside tmux):
list-keys -T copy-mode-vi
To validate in <2.4
:
list-keys -t vi-copy
The bindings should be set in the list-keys
output along with other default
options.
That’s all there is to conditional config loading in tmux! Checkout my full .tmux.conf on my github repo for specifics.