Colours with Linux terminals Thorbjørn Ravn Andersen, ravn@dit.ou.dk $Date: 1996/04/27 16:56:15 $ Most Linux distributions have a 'ls' command that supports colours, and it is often desired to have different colours than the default but this may not be a trivial task. This document explains the various aspects and approaches of altering the setup by configuring existing software, plus locations of alternative software not included with Slackware. The HTML version is also available from my own source . 1. Introduction Most Linux distributions have a 'ls' command that supports colours, and it is often desired to have different colours than the default but this may not be a trivial task. This document explains the various aspects and approaches of altering the setup by configuring existing software, plus locations of alternative software not included with Slackware. This revision is a major rewrite from the initial release, including information on xterms and kernel patching. The information in here was originally compiled with the 2.0.2 release of Slackware, and the 1.1.54 kernel. The kernel patch information was retrieved on slackware 2.2.0 with the 1.2.13 kernel, and tcsh as the default shell. 2. Which colours to choose from. This shell script (thanks to the many who sent me bash versions) shows all standard colour combinations on the current console. If no colours appear, your console does not support ANSI colour selections. #!/bin/bash # Display ANSI colours. # esc="\033[" echo -e "\t 40\t 41\t 42\t 43\t 44 45\t46\t 47" for fore in 30 31 32 33 34 35 36 37; do line1="$fore " line2=" " for back in 40 41 42 43 44 45 46 47; do line1="${line1}${esc}${back};${fore}m Normal ${esc}0m" line2="${line2}${esc}${back};${fore};1m Bold ${esc}0m" done echo -e "$line1\n$line2" done The foreground colour number is listed to the left, and the background number in the box. If you want bold characters you add a "1" to the parameters, so bright blue on white would be "30;40;1". The whole ANSI selection sequence is then ESC [ 3 0 ; 4 0 ; 1 m Note: The background currently cannot be bold, so you cannot have yellow (bold brown) as anything but foreground. This is a hardware limitation. 3. How to configure colours with ls The standard Slackware setup creates the environment variable LS_COLORS from the $HOME/.dir_colors system standard). The LS_COLORS looks like LS_COLORS=:no=00:fi=00:di=01:ln=01;36:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:.. If you want a setup different from the standard, copy /etc/DIR_COLORS to $HOME/.dir_colors seen with the script above. Use eval `dircolors -t` # set up color-ls variables to recalculate LS_COLORS with tcsh. See dircolors(1) for details. Note: If you use a 'ls' from a version of Slackware before 2.2, you should upgrade. It is buggy. 4. How to configure another default than white-on-black You will need to tell the terminal driver code that you want another default. There exists no standard way of doing this, but in case of Linux you have the setterm program. "setterm" uses the information in the terminal database to set the attributes. Selections are done like setterm -foreground black -background white -store where the "-store" makes it the default for the current console as well. This requires that the current terminal (TERM environment variable) is described "well enough" in the termcap database. If you are out of luck, well here is the gory details: 4.1. Xterm Use one of xterm -fg white -bg blue4 color_xterm -fg white -bg blue4 where 'color_xterm' supports the colour version of 'ls'. This particular choice resembles the colours used on an SGI. 4.2. Virtual console. You may modify the kernel once and for all, as well as providing a run-time default for the virtual consoles with an escape sequence. I strongly recommend the kernel patch if you have compiled your own kernel. The kernel source file is /usr/src/linux/drivers/char/console.c around line 1800, where you should modify def_color = 0x07; /* white */ ulcolor = 0x0f; /* bold white */ halfcolor = 0x08; /* grey */ as appropriate. I use white on blue with def_color = 0x17; /* white */ ulcolor = 0x1f; /* bold white */ halfcolor = 0x18; /* grey */ The numbers are the attribute codes used by the video card: the most significant digit (the "1" in the colours) is the background; the least significant the foreground. 0 = black, 1 = blue, 2 = green, 3 = cyan, 4 = red, 5 = purple, 6 = brown/yellow, 7 = white. Add 8 to get "bright" colours. Note that, in most cases, a bright background == blinking characters, dull background. (From sjlam1@mda023.cc.monash.edu.au). You may also supply a new run-time default for a virtual console, on a per-display basis with the non-standard ANSI sequence (found by browsing the kernel sources) ESC [ 8 ] which sets the default to the current fore- and background colours. Then the Reset Attributes string (ESC [ m) selects these colours instead of white on black. You will need to actually echo this string to the console each time you reboot. Depending on what you use your Linux box for, several places may be appropriate: 4.2.1. /etc/issue This is where "Welcome to Linux xx.yy" is displayed under Slackware, and that is a good choice for stand-alone equipment (and probably be a pestilence for users logging in with rlogin). This file is created by /etc/rc.d/rc.S at boot time, and you should modify (around line 75) echo ""> /etc/issue echo Welcome to Linux `/bin/uname -a | /bin/cut -d\ -f3`. >> /etc/issue to ESCAPE="" echo "${ESCAPE}[H${ESCAPE}[37;44m${ESCAPE}[8]${ESCAPE}[2J"> /etc/issue echo Welcome to Linux `/bin/uname -a | /bin/cut -d\ -f3`. >> /etc/issue This code will home the cursor, set the colour (here white on blue), save this selection and clean the rest of the screen. The modification takes effect after the next reboot. Remember to insert the _literal_ escape character in the file with C-q in emacs or control-v in vi, as apparently the sh used for executing this script does not understand the /033 syntax. 4.2.2. /etc/profile or .profile if [ "$TERM" = "console" ]; then echo "\033[37;44m\033[8]" # # or use setterm. setterm -foreground white -background blue -store fi 4.2.3. /etc/login or .login if ( "$TERM" == "console" ) then echo "\033[37;44m\033[8]" # or use setterm. setterm -foreground white -background blue -store endif 4.3. Remote login You should be able to use the setterm program as shown above. Again, this requires that the remote machine knows enough about your terminal, and that the terminal emulator providing the login supports colour. If you encounter a successful combination, I would like to have it here. 5. Software All the information described here is assuming a Slackware installation. If you have something else (like e.g. a Sun running X or so) you can get and compile the actual software yourself. The colour version of 'xterm' is based on the standard xterm source with a patch available from any X11R6 site. ftp://ftp.denet.dk/pub/X11/contrib/utilities/color-xterm-R6pl5-patch.gz See the documentation if you use an older version of X. Note: I haven't tried this myself! of the several mirrors. You need the package plus a patch. ftp://ftp.denet.dk/pub/gnu/fileutils-3.12.tar.gz ftp://ftp.funet.fi/pub/Linux/sunsite/utils/file/color-ls-3.12.0.3.patch.gz I have myself successfully compiled color-ls on Solaris, SunOS and Irix. Note that the patch requires a recent version of the patch program (pointers to source?) and you may find it the most easy to untar and patch on a Linux system and then move the source. I would appreciate feedback on this text. My e-mail address is ravn@dit.ou.dk --- Thorbjørn Ravn Andersen