【问题标题】:Enabling mouse support across different tmux versions在不同的 tmux 版本中启用鼠标支持
【发布时间】:2015-12-09 19:58:41
【问题描述】:

我管理着几台 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 没有显示任何错误,但它也没有启用鼠标模式。

我的逻辑中是否有一些错误导致此配置无法正常工作?

【问题讨论】:

    标签: tmux


    【解决方案1】:

    以最后两个答案为基础,但替换 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.8 版上,set-window-option -g mode-mouse on 为我工作。
    【解决方案2】:

    看来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
    

    对于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


    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:[[ `tmux -V |cut -d ' ' -f2` -ge 2.1 ]] 中的-ge 似乎只在比较两个整数时才有效,我不太确定。

    【讨论】:

    • 非常感谢您的帮助。我终于有机会尝试一下你的想法。虽然它是健全的,但它不起作用。我最终发现 Debian 在 backports 中有 tmux 2.1,所以我可以为 Arch 和 Debian 使用单个 .tmux.conf 文件。然而,Ubuntu 仍然停留在 2.1 之前,所以我想我要么必须等待 16.04,要么从源代码编译。也许您发布的代码可以由其他人重新编写,以便为其他正在寻找解决此问题的方法的人提供解决方案。
    • @douglas-su:tmux -V | awk '{print ($2 &gt;= 2.1)}' 似乎有问题,因为它没有正确比较版本号:tmux -V # =&gt; tmux 2.1 仍然在我运行 tmux -V | awk '{print ($2 &gt;= 2.9)}' # 1 时结果等于 1 但不应该.
    • 更大的问题是tmux -V | awk '{print ($2 &gt;= 2.1)}' 也适用于 tmux 2.0
    • 很好的解决方案,但我不认为It seems that set is not a command of tmux 是真的。使用以下配置if-shell "true" 'setw -g mode-mouse on; set -g mouse-select-window on; set -g mouse-select-pane on;' 激活鼠标模式。 (在 tmux 2.0 和 2.1 中)
    【解决方案3】:

    根据@douglas-su 的回答,我找到了一个目前有效的解决方案(请参阅下面的警告)。

    按照他的回答的步骤 1 + 2:创建两个文件,其中包含 = 2.1 选项的选项。在您的.tmux.conf 中插入以下 sn-p,而不是第 3 步:

    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 >= 21 那样工作。

    希望这会有所帮助。

    【讨论】:

    • 我尝试了该解决方案,但不幸的是,这两个文件最终都获得了来源,并且每次启动 tmux 时都会看到错误。
    • 你运行的是哪个版本的 tmux? tmux -V | cut -d ' ' -f2 | sed 's/[a\.]//g' 的输出是什么?
    • 抱歉@JayLaCroix 回复晚了。不幸的是,除了“在我的机器上工作”之外,我不知道问题可能是什么:-/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    相关资源
    最近更新 更多