似乎 Vim 正在向您的终端发送控制序列,而后者不理解。
更具体地说,您在 OP 中提到的序列:
^[[38;2;165;42;42m
^[[38;2;0;0;255m
看起来他们正在为文本编码前景色。
你可以找到他们的语法here:
CSI Pm m
其中CSI 代表“控制序列引入器”,由键ESC [ 产生,Pm 代表:
由任意数量的单个数值参数组成的多数值参数,用 ; 分隔字符。
如果您向下滚动页面,您应该会找到更详细的语法说明:
CSI Pm 字符属性 (SGR)。
...
支持 ISO-8613-6 的此变体以与 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个解释:
- 终端不支持真彩色
- tmux 未正确配置为支持真彩色
- 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
^^^^
如果没有,请查看$TERM 在 tmux 外部的输出:
$ 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