TMUX 窗格中的 vim 发生了什么?

What's happening here with vim inside TMUX pane?

当我在 TMUX 面板中打开 vim 时,该面板充满了我不认识的代码。如果我只是 运行 vim,我会得到这个:

^[[38;2;165;42;42m  1
^[[38;2;0;0;255m~

如果我用 vim 打开一个文件,我会得到这样的东西(顶部窗格):

vim 和 TMUX 都很新。我该如何解决?

似乎 Vim 正在向您的终端发送后者不理解的控制序列。
更具体地说,您在 OP 中提到的序列:

^[[38;2;165;42;42m
^[[38;2;0;0;255m

看起来他们正在为文本编码前景真实颜色。

你可以找到他们的语法 here:

CSI Pm m

其中CSI代表“Control Sequence Introducer”,由按键ESC [产生,Pm代表:

A multiple numeric parameter composed of any number of single numeric parameters, separated by ; character(s).

如果向下滚动页面,您应该会找到更详细语法的描述:

CSI Pm m Character Attributes (SGR).

...

This variation on ISO-8613-6 is supported for compatibility with KDE konsole:

Pm = 3 8 ; 2 ; Pr; Pg; Pb
Set foreground color to the closest match in xterm's palette for
the given RGB Pr/Pg/Pb.

Pm = 4 8 ; 2 ; Pr; Pg; Pb
Set background color to the closest match in xterm's palette for
the given RGB Pr/Pg/Pb.*

应用于你的第一个序列,你可以像这样分解它:

┌ CSI
│  ┌ Pm
├─┐├────────────┐
^[[38;2;165;42;42m
        ├─┘ ├┘ ├┘
        │   │  └ Pb = amount of blue
        │   └ Pg = amount of green
        └ Pr = amount of red

如果终端不理解这个序列,我可以看到3个解释:

  1. 终端不支持真彩色
  2. tmux 未正确配置以支持真彩色
  3. Vim 未正确配置以支持真彩色

要测试 1. 是否是问题所在,您可以在 ~/.bashrc:

中编写此 bash 函数
truecolor() {
  local i r g b
  for ((i = 0; i <= 79; i++)); do
    b=$((i*255/79))
    g=$((2*b))
    r=$((255-b))
    if [[ $g -gt 255 ]]; then
      g=$((2*255 - g))
    fi
    printf -- '\e[48;2;%d;%d;%dm \e[0m' "$r" "$g" "$b"
  done
  printf -- '\n'
}

然后在 tmux 外部的 shell 中执行 $ truecolor。如果你得到某种彩虹,你的终端支持真彩色(至少部分支持)。 如果您看到一些单元格未着色,而其他单元格随机着色,则您的终端不支持真彩色。

或者,您可以手动尝试序列:

$ printf '\e[38;2;%d;%d;%dm this text should be colored \e[0m' 165 42 42
$ printf '\e[38;2;%d;%d;%dm this text should be colored \e[0m' 0 0 255

如果 $ truecolor 没有产生彩虹,或者如果 $ printf 命令没有改变文本的前景色(而不是背景色),您将不得不:

  • 在您的 ~/.vimrc 中禁用 'termguicolors';即删除 set termguicolors(或使其执行 set notermguicolors
  • 尝试升级您的终端
  • 找到支持真彩色的another terminal

要测试 2. 是否是问题所在,在 tmux 中,您可以执行此 shell 命令:

$ tmux info | grep Tc

如果输出包含[missing]:

203: Tc: [missing]
         ^^^^^^^^^

这意味着 tmux 没有配置为支持真彩色。 在这种情况下,您必须在 ~/.tmux.conf:

中包含类似的内容
set -as terminal-overrides ',*-256color:Tc'
     ││ ├────────────────┘   ├────────┘ ├┘
     ││ │                    │          └ tell tmux that the terminal suppors true colors
     ││ │                    └ configure the option only if `$TERM` ends with the string `-256color`
     ││ └ the option to configure is `terminal-overrides` (see `$ man tmux`)
     │└ the next option is a server option
     └ append the value to the tmux option instead of overwriting it

然后重启tmux,执行$ tmux info | grep Tc。这次输出应该包含 true:

203: Tc: (flag) true
                ^^^^

如果没有,请查看 tmux 外部 $TERM 的输出:

$ echo $TERM

输出应与您在 :Tc 之前编写的任何模式相匹配。
在前面的示例中,我使用了模式 *-256color,它将匹配 $TERM 以字符串 -256color 结尾的任何终端。如果它与你的 $TERM 不匹配,你可以尝试其他模式,或者简单地写 * 来描述任何类型的终端:

set -as terminal-overrides ',*:Tc'

要测试 3. 是否是问题所在,您可以在 ~/.vimrc:

中编写这些命令
set termguicolors
let &t_8f = "\<Esc>[38:2:%lu:%lu:%lum"
let &t_8b = "\<Esc>[48:2:%lu:%lu:%lum"

或:

set termguicolors
let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"

两个版本之间的唯一区别是序列参数之间的分隔符。第一个版本中的冒号,第二个版本中的分号。有关详细信息,请参阅 :h xterm-true-color

您可以通过依次执行来查看这3个选项的当前值:

:echo &tgc
:echo &t_8f
:echo &t_8b

他们应该输出:

1
^[[38:2:%lu:%lu:%lum
^[[48:2:%lu:%lu:%lum

或:

1
^[[38;2;%lu;%lu;%lum
^[[48;2;%lu;%lu;%lum