tmux 的色彩和在终端上有差异的原因是:tmux 没有为终端开启 RGB 特性,本来应该是 RGB 24 位颜色的 ANSI 转义序列被 tmux 转换为比较接近的 256 色的转义序列,再输出给终端渲染。可以通过 `tmux -vvv` 启动日志查看 RGBCOLOURS flag 确认。
tmux 也给用户提供了配置来开启 RGB 特性:set -as terminal-features ",根据自己的$TERM 值而定*:RGB"
另外不推荐 export TERM=tmux-256color 这种方式修改 TERM 环境变量,这个值一般是终端模拟器初始化的时候终端自己设置的,许多外部程序会依赖这个值查询出对应的 terminfo ,从而确定终端特性能力以及在不同输入的行为,比如对 <CTRL>_l 这些控制序列的反应。正如 kitty 在这个配置上的警告说的:
#: The value of the TERM environment variable to set. Changing this
#: can break many terminal programs, only change it if you know what
#: you are doing, not because you read some advice on "Stack Overflow"
#: to change it. The TERM variable is used by various programs to get
#: information about the capabilities and behavior of the terminal. If
#: you change it, depending on what programs you run, and how
#: different the terminal you are changing it to is, various things
#: from key-presses, to colors, to various advanced features may not
#: work. Changing this option by reloading the config will only affect
#: newly created windows.
我刚写了一篇 blog 来总结了一下 tmux 在不同终端下颜色差异的原因:
https://hmgle.github.io/terminal/tmux/color/2024/05/12/term-color.html