跨不同的tmux版本启用鼠标支持

Jay*_*oix 12 tmux

我管理了几台Linux机器,其中一些在存储库中有tmux 2.1版本,另一些在tmux版本低于2.1的情况下.我使用鼠标模式,据我所知,在tmux 2.1中,启用鼠标模式的选项已更改为:

set -g mouse on
Run Code Online (Sandbox Code Playgroud)

由于我使用不同的发行版,每个发行版都有不同版本的tmux,我想制作一个.tmux.conf文件,根据版本启用相应的鼠标选项.

所以,我在.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'
Run Code Online (Sandbox Code Playgroud)

不幸的是,这不起作用.tmux不显示任何错误,但它也不启用鼠标模式.

我的逻辑中是否存在阻止此配置工作的错误?

小智 6

基于最后两个答案,但替换shell命令如下.将其添加到主配置:

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'
Run Code Online (Sandbox Code Playgroud)

这使用awk来拆分版本号,这个代码的更清晰版本是:

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
Run Code Online (Sandbox Code Playgroud)

然后将配置拆分为两个配置文件.

lt_2.1.conf 包含

set -g mode-mouse on
set -g mouse-resize-pane on
set -g mouse-select-pane on
set -g mouse-select-window on
Run Code Online (Sandbox Code Playgroud)

gt_2.1.conf 包含

set -g mouse-utf8 on
set -g mouse on
Run Code Online (Sandbox Code Playgroud)


Dou*_* Su 1

看来这set不是 tmux 的命令,你不能在if-shell.

我有一个替代方案:

  1. 在某处创建两个配置文件。这里我们假设这两个配置文件是tmux_ge_21.conftmux_lt_21.conf,它们都位于~/.tmux/目录下。

  2. 将以下内容填充到这两个文件中:

为了tmux_ge_21.conf

set -g mouse-utf8 on
set -g mouse on
Run Code Online (Sandbox Code Playgroud)

为了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
Run Code Online (Sandbox Code Playgroud)
  1. 将以下行添加到您的.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'
    
    Run Code Online (Sandbox Code Playgroud)
  2. tmux source ~/.tmux.conf在您的终端中执行。


顺便说一句:对于大于 2.1 的 tmux,鼠标滚动的默认行为已更改。如果你想让它像以前一样工作,你必须安装这个 tmux 插件: https: //github.com/nhdaly/tmux-scroll-copy-mode

如果您使用此插件,请附加set -g @plugin 'nhdaly/tmux-scroll-copy-mode'tmux_ge_21.conf.


BTW2:-ge似乎[[ `tmux -V |cut -d ' ' -f2` -ge 2.1 ]]只有在比较两个整数时才起作用,我不太确定。