The 256 color mode of xterm

Changes

16 colors

Most terminals are capable of displaying 16 colors, using escape sequences like echo -e "\033[1;31mRed". You can choose from 8 colors and print them with 2 different brightness values. Background color only supports one brightness level. This sums up to 128 combinations of foreground and background color. Here's a small bash script which prints a table of escape sequences: colortable16.sh:


Example: To get bright, red text on white background use this sequence: echo -e "\033[47m\033[1;31mBright red on white.\033[0m" The last sequence resets colors to the terminal default.

xterm's 256 color extension

xterm features an extension which can be enabled via ./configure --enable-256-color. As far as i know this is default on Gentoo Linux and disabled in Arch Linux. Because of that, I've built my own xterm from the xorg-x11 sources. You can test your terminal's 256 color capabilities with this perl script from http://www.vim.org. Here's a copy just in case that the previous link should be broken: xterm-colortest. A less detailled test is produced by this smaller perl script, which is part of the xorg package: 256colors2.pl. If your xterm is compiled with the correct configure option, you should see something like this:

As you see, this is the full 256 color palette of xterm. The first 16 colors are the default terminal colors. Additionally, there's a 6x6x6 color cube, and 24 grayscale tones. The exact RGB values of all colors are given by the above perl script from vim.org.

Escape codes for the extension

To use foreground and background colors from the extension, you only have to remember two escape codes:
Set the foreground color to index N: \033[38;5;${N}m
Set the background color to index M: \033[48;5;${M}m
Check the "vim" section below for a small C program that matches RGB colors to xterm escape sequences.

Applications that support 256 colors - vim

To make vim aware of a present 256 color extension, you can either set the $TERM environment variable to xterm-256color or use vim's -T option to set the terminal. I'm using an alias in my bashrc to do this. At the moment I only know of two colorschemes which is made for multi-color terminals like urxvt (88 colors) or xterm: inkpot and desert256, both available on vim.org.

GVim colorschemes are not prepared for 256 color consoles though it's possible to convert them. The :hi command of (g)vim accepts either color values for the gui version or for text terminals.
gvim highlight: hi Normal guifg=#cccccc guibg=#3f3f3f
vim highlight: hi Normal ctermfg=252 ctermbg=237 term=standout

If you convert a scheme or create a new one from scratch, you can distinct between the two vim versions like this: if has("gui_running") ... else ... endif like the author of inkpot did.

Screenshot of the converted blackdust color scheme:

Here are a few converted, working xterm-256-themes. Note that they don't work with gvim or 16 color consoles!

Another conversion which wasn't done by me is desert256. "inkpot" also works with 88/256 text terminals.

Applications that support 256 colors - screen

By default, screen is not aware that it is running in a 256 color capable xterm. To make programs in screen recognize this feature, you need to set three things in your ~/.screenrc:

# 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"

If this doesn't work for you, your version of screen probably wasn't compiled with ./configure --enable-colors256. Personally, I have ~/bin in my $PATH where the custom xterm/screen reside. From now on, vim should look exactly the same in screen than in the plain xterm.

Applications that support 256 colors - TinyFugue (MUD client)

TinyFugue-5.0b7 worked out of the box. Just try: /load testcolor.tf

Contact/Bugs/etc.

Last change, 2006-01-20
Wolfgang Frisch, xororand AT unfoog de