Strange behavior of vim color inside screen with 256 colors

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


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.