52

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
FJDU
  • 1,425
  • 2
  • 15
  • 21
  • Did you try your configuration with another terminal emulator? I tried your options with gnome-terminal and xterm. It work ok with both no matter how many sessions i opened. – Pablo Castellazzi Jul 22 '11 at 09:42
  • Both screen and tmux [are known](http://superuser.com/questions/145475/terminal-proxy-or-screen-without-terminal-emulation) to spoil hues. If you don't need advanced screen features but only protection from accidental terminal closing you can use dtach. For all colors I saw hues are spoiled not much, so maybe you should instead change a terminal emulator. – ZyX Jul 22 '11 at 15:31
  • Thanks for answering! After having been waiting for almost one day, the problem finally reappears. But I still don't know what actually triggered this problem. – FJDU Jul 22 '11 at 17:25
  • 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 when I attach that screen session in putty again, the colors crash: they appear as if their are only 8 colors. – FJDU Jul 24 '11 at 06:42
  • Probably not the source of your problem, but setting `term xterm-256color` in .screenrc is wrong. It tells applications running in screen that they're running in xterm, when of course they're running inside screen. It should be set to `screen-256color`. Also, you can leave out the `termcapinfo` line if you set `TERM` to `xterm-256color` before invoking screen to tell it that 256 colours are available. – ak2 Jul 24 '11 at 08:12

3 Answers3

89

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.

Max Cantor
  • 8,229
  • 7
  • 45
  • 59
  • 1
    Thanks for such a detailed answer! – FJDU Aug 03 '11 at 09:36
  • 14
    You're welcome! It represents many days spent sobbing uncontrollably while reading obtuse manpages. Hopefully my suffering can shorten someone else's learning curve! – Max Cantor Aug 04 '11 at 19:51
  • 1
    MANY thanks! Now there is only one annoying thing left in my love affair with vim: copy-pasting across servers. – Morlock Oct 30 '13 at 14:15
  • I know this is old, but I still run into a problem. Once screen has started, I still need to run `export TERM=screen-256color`, otherwise `TERM` is stuck on `xterm-256color`, which `screen` doesn't like. Any idea why? – Nicolas Mattia May 12 '15 at 12:27
  • @NicolasMattia - Did you end up figuring this out? If not, maybe try putting something obviously broken in to your `.screenrc` to make sure it's being read. – Max Cantor Aug 14 '15 at 21:42
  • I did both steps above (adding different TERM in both .bashrc and .screenrc) but when doing a 'echo $TERM' while in screen, I get 'xterm-256color'. Regardless, when I run a 'show256color' script that prints all 256 colors to my terminal while in screen it shows all 256 colors correctly. Before it would show a lot of greens and blues. – ikwyl6 Apr 01 '18 at 11:23
  • I had to set `TERM=screen-256color` instead of `TERM=xterm-256color` (which is suggested in this answer) in bash to get things working. – cchwala Aug 26 '21 at 09:33
17

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.

Nick Merrill
  • 1,242
  • 1
  • 14
  • 20
1

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.

Nikhil
  • 1,054
  • 9
  • 9