XTerm colour set-up and Molokai theme

The terminal doesn't have look boring. XTerm's colour support can bring a rainbow of effects to our directory listings!

As you'd expect XTerm has great support for 256 colour, but setting up all aspects of the terminal stack can be quite involved, so we'll look at the main elements. I use Vim primarily and want the rest of my terminal applications to use similar colours, so first we'll look at how to use Solarized which is a great theme, then how to to build up a terminal theme based on a Vim theme (Molokai). This post builds on the previous one - introduction to XTerm and TrueType fonts configuration.

XTerm with Ubuntu Mono font and Molokai theme

Figure 1: Final outcome - XTerm running Tmux with console Vim

XTerm 256 color

UNIX has been around since the 1970's so you might think colour support would be easy. But, terminal emulation is complex as it lets the user connect to systems with different capabilities. This complex capability means there are lots of pieces that can cause difficulty.

Most of us aren't using complex terminal emulation, we're just connecting to the local system over a virtual terminal. We have essentially two levels, system support and each terminal application has to support colour itself. In XTerm you can test the status of your colour support with:

$ tput colors

If it reports 256 then you're in good shape. To confirm that it can display the colours download a script that generates all the colours for you [1] and check that that they're all visible.

It's a bit clearer why application support is a challenge if you think about the stack of building blocks users interact through. For example:

  1. The shell (Bash)
    Defining a colour supporting $TERM (xterm-256color).
  2. A terminal emulator (XTerm)
    Must support 256 colors and report that support to terminal applications.
  3. A terminal multiplexer (Tmux)
    Sets $TERM to screen-256color.
  4. An application
    This could be a something interacting with the shell such as ls which has colours set-up. Or something like Vim which uses it's own terminal and colour support.

Each step in the stack must report it's settings correctly, this is generally the $TERM environmental variable but there are other aspects. For XTerm, you should check what $TERM is reporting when it's launched, and if it's not xterm-color then add this setting to .Xresources:

xterm*termName: xterm-256color

To test that colours are working in XTerm there are a couple of console applications that are useful:

Under normal circumstances this is the easiest and there are lots of themes that use colours. It's useful to test side by side in XTerm and another terminal emulator (e.g gnome-terminal), and to test under tmux and plain terminal session. I had colour differences under tmux until I got the right $TERM set-up.

Man has colour support if your pager supports it. To set it up you can do:

$ sudo apt-get install most
$ export PAGER=most
$ man man
Ranger is a vim like file manager that works in the terminal. It has colour support built-in.
ls with colour switched on displays in XTerm. It has colour support through the dircolors command which exports the LSCOLORS environmental variable. On Ubuntu there's a standard switch in the supplied .bashrc that you can uncomment to load it.
XTerm with Ubuntu Mono font and a dark grey background

Figure 2: Basic background and foreground colour

XTerm colours

XTerm has a range of settings where you have to specify a colour. You do this by providing an RGB value, the preferred way is rgb:XX/XX/XX but most people use #XXXXXX. We'll start by setting the foreground and background colours.

As before, we add lines to our .Xresources. If you look on-line you'll see examples similar to these:

! Colours for xterm
XTerm*background: #272822
XTerm*foreground: #303030
XTerm*cursorColor: #303030

The capitalisation at the start of XTerm means it's a class definition. To be more precise we can use all lower case which matches instances of the XTerm application, xterm*foreground. Either works fine. Cut and paste the setting in from above and reload X should lead to something similar to Figure 2.

As you can see it's a class hierarchy and the * lets you match intervening sections. If you're having problems with any XTerm setting make sure you don't have a general setting that's interfering. Finally, some colour themes are designed to match all X clients, for example:

! sets colors for everything.
*background: #272822
*foreground: #a08080
*cursorColor: #aaaaaa

There are a further 16 colours to define. They are split into two sets of seven: color0 to color7, and color8 to color15. The second set of colours are supposed to be bright versions of the first set, for example colour8 should be a bright version of color0.

We can use these colours to create themes, the best source for terminal colour themes is the Wayback machine of Phraktured.net. Here's one of the themes from there that works quite well:

!Theme Nine
xterm*background: rgb:1a/1a/1a
xterm*foreground: rgb:d6/d6/d6
xterm*color0:     rgb:00/00/00
xterm*color1:     rgb:9e/18/28
xterm*color2:     rgb:00/88/00
xterm*color3:     rgb:96/8a/38
xterm*color4:     rgb:41/41/71
xterm*color5:     rgb:96/3c/59
xterm*color6:     rgb:41/81/79
xterm*color7:     rgb:be/be/be
xterm*color8:     rgb:66/66/66
xterm*color9:     rgb:cf/61/71
xterm*color10:     rgb:7c/bc/8c
xterm*color11:     rgb:ff/f7/96
xterm*color12:     rgb:41/86/be
xterm*color13:     rgb:cf/9e/be
xterm*color14:     rgb:71/be/be
xterm*color15:    rgb:ff/ff/ff

xterm*cursorColor: #d6d6d6

After adding it to .Xresources, reload with xrdb -load .Xresources and launch a new XTerm. See Figure 3 which shows the theme in action, Tmux running with the top panel running ls of a directory with various file types, and ranger in the bottom panel.

XTerm showing color theme nine from Phractured

Figure 3: Theme Nine showing ls and ranger colours under tmux

As you can see ls now displays different colours. These file type colours are part of the standard install but can also be altered, see this post on dircolors [2]. You'll see in all the screenshots that there are small differences between applications choosing colours for different file types (ranger colours video files differently to ls for example), but I'm prepared to live with that!

There are lots of other themes out there such as those on Xcolors and on dotshare.it

Solarized terminal theme

The most common terminal application that I use is Vim which has a lot of colour themes. It makes sense to use a vim theme as the basis for other application's colours. It will get us closer to colour harmony across all terminal applications.

Solarized is the most popular and complete colour theme, covering a lot of editors and tools. The advantage of using Solarized is that someone has already put all the effort into creating the configuration files - yay! We're going to use the XTerm, dircolors [3] and Tmux configuration.

To set-up XTerm do the following:

Download the Solarized Xresources.dark file
Download it and save it to a file, say .Xresources.solar.
Load it into .Xresources
Somewhere in .Xresources add a line:
#include "/path/to/file/.Xresources.solar"
Load it into X's server
At the command line do:
# merges with any existing X resources
$ xrdb -merge .Xresources
# alternatively, to load and replace old entries do
$ xrdb -load .Xresources
Test it's working in XTerm
The background and foreground colours in XTerm should have changed. If you launch xclock or imagemagick you'll find that their background and foreground have also changed, because Solarized configures *background which alters all X clients. You can also run xrdb -query to see a listing of the X resources.
Set-up dircolors
The next step is to provide a ~/dircolors file so that when ls is called it uses the Solarized colours. There are some gory details about the choices depending on your terminal emulator, see the details at Seebi's github repository. The steps are:
$ wget --no-check-certificate https://raw.github.com/seebi/dircolors-solarized/master/dircolors.256dark
$ mv dircolors.256dark .dircolors
$ eval `dircolors ~/.dircolors`
Solarized terminal theme, in an XTerm displaying ls and ranger

Figure 4: Solarized theme with tmux showing ls and ranger

For initial testing you have to use the eval line to bring the new dircolors into your environment. Ubuntu will automatically load the .dircolors file as part of bash's initialisation, so to complete this step you can logout/login again. At this point when you run ls you should have different colours for file listings.

Configure Tmux for Solarized
The last step is to configure Tmux. There's a specific .tmux.conf configuration so remove any colour definitions that you have in your tmux.conf and replace them with the Solarized ones.

That should leave you with Figure 4 which you can compare to Figure 3. The Tmux theme is different and the ls output is also different. The only fly in this ointment (which I haven't been able to solve) is that the colours in ranger are different to those in ls (e.g. orphaned-soft-link).

Molokai terminal theme

I mostly use the Molokai theme in Vim as I prefer a bit more brightness. It's not as universal as Solarized as there's only a Vim theme. However, there's a slightly darker XTerm configuration by Vreon [4], which I've played with a bit.

It's quite difficult to work out how all the colours are used. It's worth recalling that the first seven colours (0-7) are supposed to be matched by bright versions (8-15), so 0->8, 1->9 etc. If you want to mess around with your own theme, this table is quite useful for examining the output from ls:

Color RGB Colour Used Example (using ls) Notes
color0 #27822 black background   Paired with color8
color1 #d0d0d0 light grey foreground, standard files /etc/fstab  
color2 #66AA11 dark green world writable directories (Do ls /), the /tmp directory has a block background of this colour  
color3 #c47f2c medium brown fifo files mkfifo fifotest Fifo files are rare so I had to make a test one to show up using ls.
color4 #30309b medium blue unused?    
color5 #7e40a5 dark purple unused? Not used by ls Ranger uses for video and mp3 Ranger uses this colour extensively, but I'm Not sure if these come from XTerm.
color6 #3579A8 medium magenta unused?   Can't see any use of it.
color7 #9999AA medium grey setuid file /usr/bin/sudo Uses color7 for text, color15 for background
color8 #303030 dark grey unused?   Should be the highlighted version of color0 but I don't see it being used.
color9 #ff0090 bright pink

orphaned soft links

gzipped files

ln -s test madeupfile


ls of orphaned soft links show differently from working soft links like ~/Examples
color10 #80FF00 bright green executable binaries /bin/ls  
color11 #ffba68 light brown (sand) block special files /dev/loop0  
color12 #5f5fee light blue Directories /home  
color13 #bb88dd light purple socket file /var/run/dbus/system_bus_socket Also used for some types of binary files.
color14 #4eb4fa light magenta symbolic link ~/Examples Any symbolic link such as /bin/lsmod Should be partnered with color6 which isn't used.
color15 #ffffff white background color, setuid /usr/bin/sudo Shows as background block color for setgid with color7 for the text Was #d0d0d0 but the contrast didn't work for me.

Using the table above, the final .Xresources.molokai file is:

! Colours are in groups as the second one should be a brighter version.

! Background: black, dark grey
#define _background #272822
#define _color8 #303030

! Color1/foreground: light grey: standard files, cursor
! Color9: bright pink: soft link e.g. ln -s test1 imaginarytest
#define _foreground #d0d0d0
#define _color9 #ff0090

! Color2: dark green: world read/write dir e.g. /tmp
! Color10: bright green: binaries user can execute /bin
#define _color2 #66AA11
#define _color10 #80FF00

! Color3: medium brown: fifo e.g mkfifo fifotest
! Color11: light brown/sand: block special files e.g ls -l /dev/loop0
#define _color3 #c47f2c
#define _color11 #ffba68

! Color4: medium blue: FIXME: unused?
! Color12: light blue: directory
#define _color4 #30309b
#define _color12 #5f5fee

! Color5: dark purple: Not used in ls, in ranger (not sure where it gets it colors from though)
! Color13: light purple: socket file /var/run/dbus_system_bus_socket
#define _color5 #7e40a5
#define _color13 #bb88dd

! Color6: medium magenta: FIXME: unused?
! Color14: light magenta: symbolic links e.g. ~/Examples /bin/lsmod
#define _color6 #3579A8
#define _color14 #4eb4fa

! Color7: medium grey: setuid file e.g. /usr/bin/sudo
! Color15: light grey: background color for setuid files e.g /usr/bin/sudo
#define _color7 #9999AA
! Trying white in case it helps
#define _color15 #ffffff
!#define _color15 #d0d0d0

! General - covering all X clients
!*background: _background
!*foreground: _foreground
! Not sure what this one does - Solarise had it at base3
!*fadeColor: XXX ! Not sure what this does

! XTerm specific
xterm*background: _background
xterm*foreground: _foreground

! Not really required - XTerm defaults to using the foreground colour
xterm*cursorColor: _foreground

xterm*color0: _background
xterm*color1: _foreground
xterm*color2: _color2
xterm*color3: _color3
xterm*color4: _color4
xterm*color5: _color5
xterm*color6: _color6
xterm*color7: _color7
! 8-15 are considered brighter colours of 1-7
xterm*color8: _color8
xterm*color9: _color9
xterm*color10: _color10
xterm*color11: _color11
xterm*color12: _color12
xterm*color13: _color13
xterm*color14: _color14
xterm*color15: _color15

I haven't gone further and configured dircolors to complement it [5]. As you can see in Figure 5 using my existing Tmux configuration, Ubuntu's standard dircolors and this theme, it comes out pretty well!

Molokai terminal theme, Xterm displaying ls and ranger

Figure 5: Molokai terminal theme with tmux panes for ls and ranger

Setting the cursor

The last bit of configuration that's worth doing is the cursor and the mouse pointer. By default, XTerm will use the foreground colour for the cursor, but there are some other options available:

! Mouse pointer
XTerm*pointerColor: white
XTerm*pointerColorBackground: black
XTerm*cursorColor: yellow
XTerm*cursorBlink: true

The problem with cursorBlink is that I have Vim set to blink the cursor when it's in INSERT mode, if you set XTerm to blink the cursor then it does it all the time the window is active.

Final thoughts

That's everything that I've found useful for configuring XTerm for general use. Although it was quite a bit of work, when you get through it XTerm looks pretty good and it's very functional.

Hopefully I've captured all the main settings required for a usable experience. There are a lot of options to explore in the manual page. If you think I've missed something important, or found the post useful, please comment below!

[1]Colour colour everywhere! 256 colour-mode for Linux consoles
[2]Bartman's page, COLORS-Lscolors, is the best short overview of the default color set-up for different file types. Ubuntu also colors a wider set of files.
[3]The answers to this question, Xterm with Solarized only showing dark background without font colors, provide a bit more insight into how dircolors and XTerm colors are related.
[4]Molokai dark .Xresources
[5]This is for Mac so is using BSD utils, but it's good. For GNU ls we have to look at DIRCOLORS. Geoff Greer's on-line color picker is useful. There's more to this than I'm willing to investigate the time understanding.

Posted in Tech Wednesday 15 June 2016
Tagged with tech linux xterm terminal