256 色屏幕内 vim 颜色的奇怪行为 [英] Strange behavior of vim color inside screen with 256 colors

查看:46
本文介绍了256 色屏幕内 vim 颜色的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让 vim 的语法高亮显示(256 色)在 screen 内工作,它在 gterm 内运行.

I was trying to make the syntax highlighting (with 256 colors) of vim work inside screen, which is running inside gterm.

一开始效果很好.我所说的一开始"是指,当我启动screen,进入vim后,颜色看起来不错,而且真的有256种颜色.

It works quite fine in the beginning. What I mean by "in the beginning" is, after I start screen, and enter vim, the colors look fine, and there are really 256 colors.

但过了一段时间(我不知道确切多久),颜色会自动变回外观,好像只有 8 种(或 16 种?)颜色.

But after a while (I don't know exactly how long) the colors automatically change back to an appearance as if there are only 8 (or 16?) colors.

例如,在这已经发生之后,如果我输入命令

For example, after this has already occurred, if I enter the command

hi Comment ctermfg=68

vim中,注释看起来是纯"绿色的;但是,如果我在屏幕外打开另一个 vim(在同一个终端中),那么使用相同的命令,注释似乎是黄色"绿色.

inside vim, the comments appear to be "pure" green; however, if I open another vim outside screen (in the same terminal), then with the same command the comments appear to be a "yellowish" green.

以下是我与颜色相关的.screenrc设置:

The following is my .screenrc settings related to color:

attrcolor b ".I"
defbce "on"
termcapinfo xterm 'Co#256:AB=E[48;5;%dm:AF=E[38;5;%dm'
term xterm-256color

运行python脚本显示所有颜色后,我发现这可能是屏幕本身的问题,与vim无关.

After running a python script to display all the colors, I find out that maybe this is a problem of screen itself, and has nothing to do with vim.

我所做的是,在有问题的screen 会话中,此脚本提供了 256 种颜色,但其中许多实际上是相同的;然而,当我使用相同的配置开始一个新的屏幕会话时,这个脚本给出了 256 种不同的颜色.

What I did is, inside the screen session with problems, this script gives 256 colors, but many of them are actually the same; however, when I start a new screen session with the same configuration, this script gives 256 colors which are distinct from each other.

昨晚我使用 putty 连接到我的 Linux 计算机(在我的办公室里,它始终处于打开状态),然后打开一个带有多个窗口的 screen 会话.昨晚的颜色是正确的.然后在我睡觉之前,我分离了 screen 会话并关闭了 putty.

Last night I connected to my Linux computer (which is in my office and it is always on) with putty, then opened a screen session with multiple windows in it. The colors are correct last night. Then before I went to sleep I detached the screen session and closed putty.

现在早上,当我再次在 putty 中附加 screen 会话时,颜色崩溃了:它们看起来好像只有 8 种颜色.

Now in the morning when I attach that screen session in putty again, the colors crash: they appear as if there are only 8 colors.

screen 外的颜色很好(但仍然在 putty 中).

The colors are fine outside screen (but still in putty).

我问这个问题三年后的今天,我看到了一个类似的问题.问题是vim可以在screen之外显示256色,而screen用测试脚本可以显示256色,但是vim不能在screen内显示任何颜色(只能显示黑白).作为对自己的说明,这是我正在使用的 .screenrc 文件

Three years later after I asked this question, today I saw a similar problem. The problem is that vim can display 256 colors outside screen, and screen can display 256 colors with a test script, but vim can't display any color (can only display black and white) inside screen. Just as a note to myself, here is the .screenrc file I am using

hardstatus alwayslastline "%{.bW}%-w%{.rW}%n %t%{-}%+w %=%{..G} %H %{..Y} %Y-%m-%d %c"
shell "bash"
startup_message off
vbell off
altscreen on
attrcolor b ".I"
defbce "on"
termcapinfo xterm* 'is=E[rE[mE[2JE[HE[?7hE[?1;4;6l'
termcapinfo xterm 'Co#256:AB=E[48;5;%dm:AF=E[38;5;%dm'
term screen-256color

已接受的答案中已经提到了问题的解决方案,即我需要包含

The solution to the problem is already mentioned in the accepted answer, namely, I need to include

export TERM=xterm-256color

.bashrc中.

推荐答案

简答

在你的 .bashrc 中将 TERM 设置为 xterm-256color,并将 term screen-256color 放入你的.screenrc.

Short Answer

Set TERM to xterm-256color in your .bashrc, and put term screen-256color in your .screenrc.

这就是为什么会出现这种情况:gnome-terminal、screen、tmux、bash、putty 和 vim 被编写为智能处理 256 种颜色,但您需要尽早正确设置观点.在您的 .screenrc 中使用 termcapinfo 实际上是一种胶带解决方案!

Here's why this breaks: gnome-terminal, screen, tmux, bash, putty and vim have all been written to intelligently handle 256 colors, but you need to set things up correctly at the earliest possible point. Using termcapinfo in your .screenrc is actually a duct tape solution!

如果您的 TERM 设置正确,它会向 bash 发出信号,表明您处于 256 色模式,这意味着它在屏幕处于 256 色模式时也能很好地播放.

If your TERM is set correctly, it will signal to bash that you're in 256-color mode, which means it will play nice with screen being in 256-color mode as well.

因此,在您的 .bashrc 中,export TERM=xterm-256color.[1]

So, in your .bashrc, export TERM=xterm-256color. [1]

在你的 .screenrc 中,使用 screen-256color 作为 TERM 而不是 xterm-256color,然后删除剩下的部分!

In your .screenrc, use screen-256color for TERM instead of xterm-256color, and delete the rest of the cruft!

在您的 PuTTy 配置中,使用 putty-256color.

In your PuTTy configuration, use putty-256color.

您可以下载 termcap 条目文件并将它们放在 ~/.terminfo/s~/.terminfo/p 中,如果您的盒子没有它们默认情况下.

You can download the termcap entry files and put them in ~/.terminfo/s and ~/.terminfo/p, if your box doesn't have them by default.

脚注

[1] 在您的 .bashrc 中将 TERM 设置为 xterm-256color 可能有点冒昧,尤其是如果您使用相同的 .bashrc 在多台机器上.我发现以下代码段相当有效:

[1] Setting TERM to xterm-256color in your .bashrc can be a little presumptuous, especially if you use the same .bashrc on multiple machines. I have found the following snippet to be fairly effective:

case "$TERM" in
*-256color)
    alias ssh='TERM=${TERM%-256color} ssh'
    ;;
*)
    POTENTIAL_TERM=${TERM}-256color
    POTENTIAL_TERMINFO=${TERM:0:1}/$POTENTIAL_TERM

    # better to check $(toe -a | awk '{print $1}') maybe?
    BOX_TERMINFO_DIR=/usr/share/terminfo
    [[ -f $BOX_TERMINFO_DIR/$POTENTIAL_TERMINFO ]] && 
        export TERM=$POTENTIAL_TERM

    HOME_TERMINFO_DIR=$HOME/.terminfo
    [[ -f $HOME_TERMINFO_DIR/$POTENTIAL_TERMINFO ]] && 
        export TERM=$POTENTIAL_TERM
    ;;
esac

ssh 的alias 是一种防御措施,可防止我们尝试在不一定支持它的远程机器上打开 256 色终端.主要块是等式的另一半;它会检查相应的 terminfo 条目是否存在,如果存在则设置它.

The alias of ssh is a defensive measure to prevent us from trying to open a 256-color terminal on a remote machine that doesn't necessarily support it. The main block is the other half of the equation; it checks to see if the corresponding terminfo entry exists, and sets it if it does.

这篇关于256 色屏幕内 vim 颜色的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆