The 256 color mode of xterm


The 256 color mode of xterm



  • 2006-01-20: Updated the converter script and added a few converted theme

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

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

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

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. 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
/033[38;5;125m af005f

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 /usr/X11R6/lib/X11/rgb.txt . 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
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


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

<script src="" type="text/javascript"> </script>

个人分类: Linux
下一篇doxygen+VIM文档实用指南for C/C-liked Programmers
想对作者说点什么? 我来说一句