linux - TMUX Pane 中的 vim 发生了什么?

标签 linux vim tmux

当我在 TMUX Pane 中打开 vim 时, Pane 中充满了我无法识别的代码。如果我只运行 vim,我会得到这个:

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

如果我用 vim 打开一个文件,我会得到这样的东西(顶部 Pane ): enter image description here

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

关于linux - TMUX Pane 中的 vim 发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53327486/

相关文章:

分离 ssh+tmux session 时,Python 代码崩溃并显示 "cannot connect to X server"

tmux - 从 tmux 命令行创建新 Pane

vim - 如何跳转到vim帮助文件中的下一个标签

vim - 在 .vimrc 中,当光标在括号/大括号之间并按下回车键时起作用

c - 在 C 中解析大文件

linux - 两个不同进程的两个相同的线性地址?

VIM:将外部命令的输出存储到寄存器中

tmux - 在 tmux 的所有窗口中显示 Pane

c - 伪终端(pty)报告资源暂时不可用

c - 使用计时器时处理 'intterupted system call' 错误