Coping With Color on the Linux Console (and XTerm and friends)

Posted by Nate Bargmann on Tue, Feb 4, 2020

In the previous post about ANSI escape sequences, terminfo, and the tput utility, what I did not mention is that even though a given color such as blue or cyan is requested, it is beyond the program’s control the actual color the user will see.  A legitimate question is why would red not be red or magenta not be magenta?  In a word, themes.  Now themes are not a bad thing as they help to give a desktop a cohesive look across applications.  Desktop theme authors desire to extend their creativity to the Linux console and that is where the trouble begins.

To back up a bit, in the previous post it was left unstated but implied that the Linux console and XTerm terminals traditionally have eight colors available for foreground (text color) and the same eight for background.  Note that it is possible that on some ancient terminals only eight colors are available or none other than gray scale (or green or amber).  Fortunately, modern POSIX terminals don’t suffer from this limitation and many support 256 colors or even more!  To keep things simple this post will consider the common case of eight foreground and eight background colors.

The eight common colors are black, red, green, yellow, blue, magenta, cyan, and white and are numbered from 0 through 7, respectively.  The assignment of the environment variables in ~/.bashrc used in the PS1 prompt and less showed this ordering.  One could reasonably expect a color scheme like this from Debian’s Linux console:

Screen capture of a Debian Linux console with its default color scheme.

Color scheme of a Debian Linux console

But then your program is run on Xubuntu‘s Linux console with a scheme like this:

Screen capture of an Xubuntu Linux console with its default color scheme.

Color scheme of an Xubuntu Linux console

For the programmer that chooses color combinations for readability, a theme like this can be a headache as there is no feedback from the Linux console or terminal emulator that tells the program what the color scheme actually looks like to the user.

As another set of examples, here is Midnight Commander in Debian and Xubuntu:

Screen capture of Midnight Commander in a Debian Linux console.

Midnight Commander in a Debian Linux console

Screen capture of Midnight Commander in an Xubuntu Linux console.

Midnight Commander in an Xubuntu Linux console

The Xubuntu theme is lower contrast than Debian and results in certain color highlights being more difficult to read.  If you rarely work in the Console, this may not matter too much, but there will probably be that one time…

Configuring the Linux console color palette

Like everything else in Linux, this too may be configured.  The Xubuntu distribution includes the setvtrgb utility that is run each time the system starts.  This utility loads a file that is managed by the Debian update-alternatives utility (the Ubuntu flavors are derivatives of Debian and so have much in common).  The following screenshot shows running the utility in interactive mode.  Select option 2 for the /etc/console-setup/vtrgb.vga file.  Reload the file using systemctrl.  These commands are shown in the screenshot:

Screen capture showing the command to set the Xubuntu console to the VGA color pallette

Setting Xubuntu console to the VGA color pallette

and voila!  “normal” console colors are the result:

Screen capture of an Xubuntu Linux console with the standard VGA color pallette.

Xubuntu Linux console with the standard VGA color pallette

Programs such as Midnight Commander appear as the programmer intended.

Regardless of whether you like a “normal” color palette, the Xubuntu theme or want to modify the Linux console colors to your own liking, use the setvtrgb utility.  The color palette will then be persistent whereas escape codes or tput set colors are not.

Now it’s time to look at GUI terminal emulators.

Desktop GUI terminal emulators

Every desktop environment provides its own GUI terminal emulator.  Each has a similar and slightly different way to configure the color palette available to programs running in the emulator.  Like the Linux console the program’s appearance can be changed dramatically depending on the color palette in use.  These emulators have settings available in the GUI to set the palette.   I’ll leave it as an exercise for the reader as to how these are configured in your favorite desktop’s terminal emulator.

Configuring colors in XTerm and rxvt

Long before there were such things as desktop environments for POSIX systems, there were terminal emulators for the X Window System.  It may be of no surprise that the granddaddy of all modern GUI terminal emulators, XTerm, is still under development.  Then rxvt came on the scene some years later as a more light weight version of XTerm with a differing feature set.  One thing they have in common is that they are configured using X Resources.  Both now support Unicode as uxterm and urxvt, respectively.

With a terminal emulator already provided by the desktop environment you might be wondering why I am bothering with these ancient terminals.  For one thing they are minimalist and as such allow more keys to be passed to the running application.  One example is F11 which most desktop provided emulators use for making the terminal full screen.  Some other keys are used to switch tabs in the terminal GUI and are thus not available to the running application.  I use XTerm or its Unicode variant UXTerm for the Tlf amateur radio logging program as it passes the most keys to Tlf.

The default Xresources file provided by XTerm does have a reasonable default color scheme:

Screen capture of the Default XTerm color scheme.

Default XTerm color scheme

Of course, I like to tweak it a bit more to my liking.  Not only will my Xresources file change the colors a bit but it will also give the cursor block a distinctive color, use an anti-aliased font, and set the geometry to 80 columns by 25 lines, the minimum size needed for the Tlf amateur radio logging program:

xterm*faceName: Monospace:size=11:antialias=true
XTerm*allowBoldFonts: true

xterm*geometry: 80x25

XTerm*termName: xterm-256color
UXTerm*termName: xterm-256color

!----------------------------------------------------------------------
! Linux terminal scheme from Gnome Terminal Linux Console color palette

! Lighter Black & Gray
xterm*background: #0c0c0c
xterm*foreground: #aaaaaa

! Black & DarkGray
xterm*color0: #000000
xterm*color8: #555555

! DarkRed & Red
xterm*color1: #aa0000
xterm*color9: #ff5555

! DarkGreen & Green
xterm*color2: #00aa00
xterm*color10: #55ff55

! DarkYellow & Yellow
xterm*color3: #aa5500
xterm*color11: #ffff55

! DarkBlue & Blue
xterm*color4: #0000aa
xterm*color12: #5555ff

! DarkMagenta & Magenta
xterm*color5: #aa00aa
xterm*color13: #ff55ff

! DarkCyan & Cyan
xterm*color6: #00aaaa
xterm*color14: #55ffff

! LightGray + White
xterm*color7: #aaaaaa
xterm*color15: #ffffff

! Orange
xterm*cursorColor: #ffa530

Copy and paste this into a file named ~/.Xresources and then run the following command to have the X server merge these changes into its resource database:

xrdb -merge ~/.Xresources

This command will need to be run each time a change is made to ~/.Xresources.  This file is read each time the X server is started, e.g., at system boot so manually merging should not be necessary.

Note: When using Wayland (most often with the Gnome desktop), the ~/.Xresources file will not be read.  According to various sources, the following command can be run manually or setup to be run at session login:

xrdb -load ~/.Xresources

The color scheme is now to my liking:

Screen capture of an XTerm with the Linux console color scheme.

XTerm with the Linux console color scheme

Of course, color values could be chosen from the desktop terminal theme that you like and put in ~/.Xresources to have its color scheme match the desktop’s terminal emulator.  The choice is yours.

Moving on to rxvt, in its default scheme, I’m not sure that is to anyone’s liking:

Screen capture of the default rxvt color scheme.

Default rxvt color scheme

Ouch!

This one too can be rectified by appending the following to ~/.Xresources and merging:

!----------------------------------------------------------------------
! URxvt settings

URxvt.geometry: 80x25
URxvt.font: xft:Monospace:size=11:antialias=true

! URxvt VGA colors

! Orange
URxvt.cursorColor: #ffa530
!URxvt.pointerColor: #dc74d1

! Lighter Black & Gray
URxvt.background: #0c0c0c
URxvt.foreground: #aaaaaa

! Black & DarkGray
URxvt.color0: #000000
URxvt.color8: #555555

! DarkRed & Red
URxvt.color1: #AA0000
URxvt.color9: #FF5555

! DarkGreen & Green
URxvt.color2: #00AA00
URxvt.color10: #55FF55

! DarkYellow & Yellow
URxvt.color3: #AA5500
URxvt.color11: #FFFF55

! DarkBlue & Blue
URxvt.color4: #0000AA
URxvt.color12: #5555FF

! DarkMagenta & Magenta
URxvt.color5: #AA00AA
URxvt.color13: #FF55FF

! DarkCyan & Cyan
URxvt.color6: #00AAAA
URxvt.color14: #55FFFF

! LightGray + White
URxvt.color7: #AAAAAA
URxvt.color15: #FFFFFF

Ahhh, much better:

Screen capture of rxvt with the Linux console color scheme.

rxvt with the Linux console color scheme

Each terminal has many settings available to tweak through the Xresources file.  Consult the man page of each for more information.

Conclusion

As I do quite a bit in the various terminal emulators and occasionally in the Linux console, I prefer a consistent color scheme among the lot of them.  This post gives some guidance on how this can be achieved whether to match my preference, a desktop theme, or something completely different.  The choice is yours.

Have fun!