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

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

问题描述

我试图让vim的语法高亮显示(256种颜色)在screen内部运行,而screengterm内部运行.

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.

现在早晨,当我再次将screen会话附加到putty中时,颜色崩溃:它们看起来好像只有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.就像给自己的笔记一样,这是我正在使用的.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[r\E[m\E[2J\E[H\E[?7h\E[?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,然后在.screenrc中放置term screen-256color.

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.

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

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