Sunday, November 11, 2012

256 colors in console vim

To use 256 colors in vim it is needed to correctly set $TERM variable. For gnome-terminal it is gnome-256color. vim uses $TERM variable and terminfo files to determine terminal properties. To determine correct value of $TERM you can add to your .bashrc this code from vim.wikia.com:

if [ "$TERM" = "xterm" ] ; then
    if [ -z "$COLORTERM" ] ; then
        if [ -z "$XTERM_VERSION" ] ; then
            echo "Warning: Terminal wrongly calling itself 'xterm'."
        else
            case "$XTERM_VERSION" in
            "XTerm(256)") TERM="xterm-256color" ;;
            "XTerm(88)") TERM="xterm-88color" ;;
            "XTerm") ;;
            *)
                echo "Warning: Unrecognized XTERM_VERSION: $XTERM_VERSION"
                ;;
            esac
        fi
    else
        case "$COLORTERM" in
            gnome-terminal)
                # Those crafty Gnome folks require you to check COLORTERM,
                # but don't allow you to just *favor* the setting over TERM.
                # Instead you need to compare it and perform some guesses
                # based upon the value. This is, perhaps, too simplistic.
                TERM="gnome-256color"
                ;;
            *)
                echo "Warning: Unrecognized COLORTERM: $COLORTERM"
                ;;
        esac
    fi
fi

SCREEN_COLORS="`tput colors`"
if [ -z "$SCREEN_COLORS" ] ; then
    case "$TERM" in
        screen-*color-bce)
            echo "Unknown terminal $TERM. Falling back to 'screen-bce'."
            export TERM=screen-bce
            ;;
        *-88color)
            echo "Unknown terminal $TERM. Falling back to 'xterm-88color'."
            export TERM=xterm-88color
            ;;
        *-256color)
            echo "Unknown terminal $TERM. Falling back to 'xterm-256color'."
            export TERM=xterm-256color
            ;;
    esac
    SCREEN_COLORS=`tput colors`
fi

if [ -z "$SCREEN_COLORS" ] ; then
    case "$TERM" in
        gnome*|xterm*|konsole*|aterm|[Ee]term)
            echo "Unknown terminal $TERM. Falling back to 'xterm'."
            export TERM=xterm
            ;;
        rxvt*)
            echo "Unknown terminal $TERM. Falling back to 'rxvt'."
            export TERM=rxvt
            ;;
        screen*)
            echo "Unknown terminal $TERM. Falling back to 'screen'."
            export TERM=screen
            ;;
    esac
    SCREEN_COLORS=`tput colors`
fi

Also you have to have corresponding terminfo files. On Ubuntu you can install additional terminfo definitions:

sudo apt-get install ncurses-term
Now you have gnome-256color and other terminal definitions in /usr/share/terminfo (directory depends on system). You can use such themes as desert256, wombat256 etc.

To set colorscheme I use this code in my .vimrc:

if has("gui_running")
    colorscheme wombat
else
    colorscheme wombat256
endif

You can check how many colors are used inside vim by examining t_Co variable:

set t_Co
t_Co=256
I meet a difficulty with setting $TERM on one of Solaris servers I use. I had not colors in vim at all on this server. tput colors said me -1. There are not any terminfo file like *-256color and I have not root priviledges to add needed terminfo files. $TERM was set to xterm. But I found xtermc in terminfo directory. After setting $TERM to xtermc tput colors gives 8, and I can use syntax highlighting in vim, although not 256 colors.

No comments:

Post a Comment