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

时间:2015-12-09 19:58:42

标签: tmux

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

set -g mouse on

由于我使用不同版本的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'

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

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

3 个答案:

答案 0 :(得分: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'

这使用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

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

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

gt_2.1.conf包含

set -g mouse-utf8 on
set -g mouse on

答案 1 :(得分:1)

似乎set不是tmux的命令,您无法在if-shell中执行它。

我有另一种方案:

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

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

  3. tmux_ge_21.conf

    set -g mouse-utf8 on
    set -g mouse on
    

    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. .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. 在您的终端中执行tmux source ~/.tmux.conf

    3. BTW:对于大于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 ]]似乎仅在比较两个整数时起作用,我不太确定。

答案 2 :(得分:0)

在@ douglas-su的答案的基础上,我找到了一个目前有效的解决方案(见下面的警告)。

按照他的回答的第1 + 2步:创建两个文件,其中包含&lt; 2.1和&gt; = 2.1选项。而不是第3步,请在.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'

说明:

  • cut -d ' ' -f2选择tmux -v的第二部分。示例:为'tmux 2.1'
  • 返回'2.1'
  • sed 's/[a\.]//g'替换版本字符串中的所有点.a。示例:为“1.9a”
  • 返回19

警告:这个解决方案可能不适用于所有永恒,但到目前为止应该适用于所有releases of tmux(当前版本为2.1)。如果由于任何原因发布了更新的旧版本(例如安全修复程序的2.0.1或类似内容),则建议的解决方案将不再适用于201&gt; = 21。

希望这有帮助。

相关问题