256-Color XTerms in Ubuntu «
»


Code: , , , , , ,
Comments Off on 256-Color XTerms in Ubuntu

It’s not commonly used, but most Linux terminals can support 256 colors. It’s also a bit of a pain in the ass to set up if it doesn’t Just Work out of the box. Having spent a while today tinkering and searching and cursing and testing and trading mail/IM with folks who understand the eldritch depths of terminals better than I, I thought I’d write up my findings for anyone else who’d like spiffy colors.

Ncurses and other terminal programming libraries need to know what capabilities your terminal supports, like highlighting, underlining, blinking, and colors. This used to be done with termcap but now everyone uses terminfo, which hold the specifications in a set of files. The first thing to do is make sure you have a 256-color terminfo file for your terminal:


$ find /lib/terminfo /usr/share/terminfo -name "*256*"

What you need is to see a filed named terminal-256color. For example, mine is xterm-256color. My first hassle was that Ubuntu doesn’t install this by default. I can’t guess what it will be named in your distribution (look for packages with ‘ncurses’ or ‘term’ in their names), but in Ubuntu it’s just a quick:


$ sudo aptitude install ncurses-term

Xterm needs a little more configuration, edit ~/.Xdefaults to add:


*customization: -color
XTerm*termName: xterm-256color

To make this apply to new terminals (so you don’t have to log out and back in), run:


$ xrdb -merge ~/.Xdefaults
# you'll also want to add this to your ~/.xsession so it happens every time you log in:
if [ -f $HOME/.Xdefaults ]; then
xrdb -merge $HOME/.Xdefaults
fi

One tricky thing about xterm is that is supports 256 colors via ANSI escape sequences, but they won’t be available to programs without the right terminfo file. So popular test scripts will work while ncurses-based programs will fail. I spent a lot of time catching on to this one. This config file will tell xterm to enable 256 colors and set the TERM environment variable to ‘xterm-256color’ so the right terminfo file is used. You can test this with:


$ tput colors
256
$ echo $TERM
xterm-256color

Xterm color test

At this point, you’re all set. Individual applications may need some configuring to use 256-color schemes. In vim, :set t_Co should now report that you have 256 colors available. Run locate vim | grep 256 to find the color schemes available, and :colo desert256 to use one. If none of those suit you, there are plenty more available. I put a little code in my ~/.vimrc to only load it when I have colors available, because sometimes I use different terminals.


colorscheme desert
if &t_Co > 2 || has("gui_running")
syntax on
endif
if &t_Co == 256
colorscheme inkpot
endif

Vim in 256 colors with colorscheme 'inkpot'

GNU screen needs a hint to use 256 colors. You can add the following to your ~/.screenrc, or just wait for Ubuntu Intrepid Ibex later this month to include it by default:


# terminfo and termcap for nice 256 color terminal
# allow bold colors - necessary for some reason
attrcolor b ".I"
# tell screen how to set colors. AB = background, AF=foreground
termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'
# erase background with current bg color
defbce "on"
# set TERM
term screen-256color-bce

This was all (of course) an hours-long session of yak shaving because I’m playing around with a secret project I’m not yet ready to announce.