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

发布于 2024-11-25 08:48:33 字数 1739 浏览 0 评论 0原文

我试图使 vim 的语法突出显示(256 色)在 screen 中工作,而该屏幕在 gterm 中运行。

一开始它工作得很好。我所说的“一开始”是指,在我启动 screen 并输入 vim 后,颜色看起来不错,而且确实有 256 种颜色。

但过了一会儿(我不知道具体多长时间),颜色会自动变回外观,就好像只有 8(或 16?) 颜色。

例如,在这种情况发生之后,如果我

hi Comment ctermfg=68

在 vim 中输入命令,注释将显示为“纯”绿色;但是,如果我在屏幕外打开另一个 vim(在同一终端中),则使用相同的命令,注释将显示为“黄”绿色。

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

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

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

我所做的是,在有问题的 screen 会话中,该脚本提供了 256 种颜色,但其中许多颜色实际上是相同的;但是,当我使用相同的配置启动新的屏幕会话时,此脚本提供了 256 种彼此不同的颜色。

编辑:

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

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

屏幕外颜色很好(但在putty中仍然如此)。

编辑:

在我问这个问题三年后,今天我看到了类似的问题。问题是 vimscreen 之外可以显示 256 种颜色,而 screen 用测试脚本可以显示 256 种颜色,但是 vim< /code> 在屏幕内无法显示任何颜色(只能显示黑色和白色)。就像给自己一个注释,这是我正在使用的 .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

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

export TERM=xterm-256color

在 .bashrc 中代码>.

I was trying to make the syntax highlighting (with 256 colors) of vim work inside screen, which is running inside 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.

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

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.

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

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.

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.

Edit:

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.

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.

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

Edit:

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

in .bashrc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

虚拟世界 2024-12-02 08:48:33

简短回答

.bashrc 中的 TERM 设置为 xterm-256color,并将 term screen-256color 放入 <代码>.screenrc。

长答案

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

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

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

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

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

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


脚注

[1] 在 .bashrc 中将 TERM 设置为 xterm-256color 可能有点冒昧,特别是如果您在多台计算机上使用相同的.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 $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 条目是否存在,如果存在则设置它。

Short Answer

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

Long Answer

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!

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.

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

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

In your PuTTy configuration, use putty-256color.

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.


Footnotes

[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

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.

遗心遗梦遗幸福 2024-12-02 08:48:33

Max 有一个很好的答案,但我实际上必须使用 ./configure --enable-colors256重新安装屏幕,以确保 config.h 文件设置了 #define COLORS256 1,但我的机器上默认情况并非如此。然后,我发现只要确保我的TERM设置为xterm-256color,其他设置就没有必要了。

Max has an excellent answer, but I actually had to reinstall screen with ./configure --enable-colors256 to ensure that the config.h file had #define COLORS256 1 set, 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 TERM was set to xterm-256color.

没︽人懂的悲伤 2024-12-02 08:48:33

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

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

可以使用以下方式获取最新版本的屏幕
git 克隆 https://git.savannah.gnu.org/git/screen.git

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

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

注意 4:在执行 make install 时,您需要在 Ubuntu 上具有 sudo 访问权限此步骤使用chown

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

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

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

Note 2: Unlike some of the previous versions, While configuring this version of screen there is no need to use --enable-colors256 option

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

Note 4: You will need sudo access on Ubuntu while doing make install as this step uses chown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文