The 256 color mode of xterm
- 2006-01-20: Updated the converter script and added a few converted theme
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:||
|Set the background color to index M:||
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
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
, 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.
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. To facilitate the conversion between RGB
and xterm codes, I've quickly hacked together a few lines of C(++) to
do this for us.
You can get the source (5kb) here: xterm256-conv2.tar.bz2 . To compile it, just type make. It then builds two binaries, conv-rgb2xterm, which reads 6 hexadecimal digits from stdin, tries to find the best match in xterm's color table and finally prints the corresponding bash escape sequence and matched color. Example:
xoa@rupert ~/source/xterm256-conv $ echo ab2367 |./conv-rgb2xterm
The other program (xtermize-gvim-scheme) reads a vim colorscheme from stdin and does some basic replacements, like s/gui/cterm/g/, s/guifg/ctermfg/g/, s/guibg/ctermbg/g/. It also looks for hexadecimal rgb colors and replaces them with the best-match xterm color index. It's also able to replace named colors like "DarkBlue" and colors defined in
About 2/3 of the "converted" gvim themes will need manual corrections
but most work quite fine. Would be nice to have a good visual editor
for such themes ;)
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
If this doesn't work for you, your version of screen probably wasn't compiled with
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)
worked out of the box. Just try:
Last change, 2006-01-20
Wolfgang Frisch, xororand AT unfoog de