Skip to content

tools for making full use of the 256-colour palette available to you both on the cmdline and in vi

Notifications You must be signed in to change notification settings

twoerner/xterm-256colour

Repository files navigation

256-colour xterm, vim, etc

I have been using xterm since 1994 and have found that not only does it
work well enough for me, I am quite comfortable using it and have found no
reason to switch to any other terminal emulators. After 18 years, however,
those basic 8 xterm colours are beginning to burn holes into the backs of my
retinas. Therefore I decided it was worth switching away from xterm in order to
be able to use a nicer palette of colours (especially for Vim). I was quite
thrilled, then, as I started searching for a replacement, to find that modern
xterm's happily support 256 colours (and I therefore don't need to find a
replacement), the trick is in simply using them to their full advantage.

So now I have to ask: in 2012, if xterm can support 256 colours, why do modern
distributions still ship applications and colour schemes that assume only the 8
original colours are available (16, when you include the "bold" variants, but
what does that buy you?)?

Personally I use openSuSE, but in my investigations it looks like Fedora at
least makes a stab at using the 256 colour palette.

In my day-to-day use of Linux, I tend to use an xterm running bash, and Vim
quite a lot. As such there are several places where I like to use colours:
- to distinguish between different file types (using 'ls')
- for Vim colour schemes
- for my bash command prompts (e.g. PS1)

Obviously a 256-colour palette isn't going to provide you with every hue of
(for example) lavender you might be interested in seeing. But the 256-colour
palette does provide for a range of different hues and saturations. If
you'd like to see what colours your terminal supports simply run the
'xtermcolours.sh' script. It queries your settings and then prints out each
colour number in the colour of the given number.

If you are disappointed to find your terminal only supports 16 colours, try
setting your TERM environment variable to 'xterm-256color' and see if that
doesn't give you better results.

If you have an existing Vim colour scheme file (especially the schemes that are
put together assuming a GUI-based Vim) and you want to convert it to the
nearest approximations of colours supported by a 256-colour xterm, you'll need
to be able to convert both colour hex triplets (e.g.  #0055aa) and colour names
(e.g.  "light salmon 2") into the closest xterm colour index.

To convert a colour name to the nearest xterm index try 'colourname2xterm.sh':

	$ colourname2xterm.sh "Light Salmon 2"
	209

Or to convert the hex to the nearest xterm index:

	$ rgb2xterm '#ee9572'
	209

If you'd like to convert a colour name to a hex triplet:

	$ colourname2hex.sh "Light Salmon 2"
	#EE9572

To get nicer bash command prompts (e.g. PS1) you can use the 'setaf' program
to define the escape sequences required. For example, if you want to set your
'blue' to be 'peacock' you can start with:

	$ colourname2xterm.sh peacock
	74

Now that we know we want index 74 for our "blue" we can edit our ~/.bashrc to
contain something like the following:

	_blue="$(tput setaf 74)"
	_noattr="$(tput sgr0)"
	...
	PS1='\[$_blue\][\u@\h \W]$ \[$_noattr\] '

Another place where colour can sometimes help is when using 'ls'.
Distributions have long been including a utility called 'dir_colors' which
will define the LS_COLORS environment variable which is used by 'ls'. By
default openSuSE comes with a /etc/DIR_COLORS file that only assumes 16
colours (i.e. it sets foreground colours in the 30's and background colours
in the 40's). By updating the file to use '38;5;<256-colour index>' you can
specify any colour from the 256-index palette for any foreground colour, or
use '48;5;<colour>' to specify background colours.

About

tools for making full use of the 256-colour palette available to you both on the cmdline and in vi

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published