bash 256 色屏幕内 vim 颜色的奇怪行为

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6787734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:46:57  来源:igfitidea点击:

Strange behavior of vim color inside screen with 256 colors

bashvimcolorsterminalgnu-screen

提问by FJDU

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

我试图让语法高亮(256色)的vim工作中screen,这是运行的内部gterm

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.

它在开始时工作得很好。我所说的“一开始”是指,在我开始screen并进入后vim,颜色看起来不错,实际上有 256 种颜色。

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.

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

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

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

hi Comment ctermfg=68

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

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

The following is my .screenrc settings related to color:

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

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

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.

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

What I did is, inside the screensession 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.

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

Edit:

编辑:

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

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

Now in the morning when I attach that screensession in puttyagain, the colors crash: they appear as if there are only 8 colors.

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

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

外面的颜色很好screen(但仍然在putty)。

Edit:

编辑:

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

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

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

in .bashrc.

.bashrc

回答by Max Cantor

Short Answer

简答

Set TERMto xterm-256colorin your .bashrc, and put term screen-256colorin your .screenrc.

在您的 中设置TERM为,然后在您的.xterm-256color.bashrcterm screen-256color.screenrc

Long Answer

长答案

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

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

If your TERMis 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.

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

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

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

In your .screenrc, use screen-256colorfor TERMinstead of xterm-256color, and delete the rest of the cruft!

在您的 中.screenrc,使用screen-256colorforTERM而不是xterm-256color,并删除其余的 cruft!

In your PuTTy configuration, use putty-256color.

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

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

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



Footnotes

脚注

[1] Setting TERMto xterm-256colorin your .bashrccan be a little presumptuous, especially if you use the same .bashrcon multiple machines. I have found the following snippet to be fairly effective:

[1]设置TERMxterm-256color.bashrc可以稍微放肆,特别是如果你使用相同的.bashrc多台计算机上。我发现以下代码段相当有效:

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 }') 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

The aliasof 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.

aliasSSH的是阻止我们试图打开一个远程计算机并不一定支持它在256色终端防御措施。主要块是等式的另一半;它检查是否存在相应的 terminfo 条目,如果存在则设置它。

回答by Nick Merrill

Max has an excellent answer, but I actually had to reinstallscreen with ./configure --enable-colors256to ensure that the config.hfile had #define COLORS256 1set, which was not the case by default on my machine. Then, I found that the other settings were not necessary so long as I ensured that my TERMwas set to xterm-256color.

Max 有一个很好的答案,但实际上我不得不重新安装screen./configure --enable-colors256以确保config.h文件已#define COLORS256 1设置,这在我的机器上默认情况下并非如此。然后,我发现只要我确保将 myTERM设置为xterm-256color.

回答by Nikhil

In the latest version of screen (v4.99.0), there is no need to use term screen-256colorin your .screenrc. Even without this setting, the vim color inside and outside screen is exactly the same.

在最新版本的 screen (v4.99.0) 中,term screen-256color您的.screenrc. 即使没有这个设置,屏幕内外的vim颜色也是完全一样的。

Note 1: I have tested this feature on Mac OS High Sierra 10.13.4 and Ubuntu 16.04.

注 1:我已在 Mac OS High Sierra 10.13.4 和 Ubuntu 16.04 上测试过此功能。

One can obtain the latest version of screen using git clone https://git.savannah.gnu.org/git/screen.git.

可以使用 获取最新版本的屏幕 git clone https://git.savannah.gnu.org/git/screen.git

Note 2: Unlike some of the previous versions, While configuringthis version of screen there is no need to use --enable-colors256option

注2:与之前的一些版本不同,在配置此版本的屏幕时,无需使用--enable-colors256选项

Note 3: You need to have PAM support for this configuration to be successful. In ubuntu, you can use sudo apt-get install libpam0g-dev

注意 3:您需要有 PAM 支持才能使此配置成功。在 ubuntu 中,您可以使用sudo apt-get install libpam0g-dev

Note 4: You will need sudoaccess on Ubuntu while doing make installas this step uses chown.

注意 4:sudo在执行make install此步骤时,您将需要在 Ubuntu 上访问chown