Add a splash of color to your command line environment

By default, much of the text in a Linux terminal is just black letters on a white background. While it has a certain peaceful aesthetic about it, this wall of text is needlessly difficult to “parse” with your eyes. Everyone I know that remained sane while using the terminal for extended periods of time has configured the thing to their personal taste.

As you probably know already, your terminal can display colors. Color can be leveraged to add another dimension of information to the text on your screen and provide helpful marks so you can easily see what’s going on.

Modern terminal emulators (the program that is showing your command prompt) can display 256 colors. When we configure things, we must refer to these colors by number:

Terminal colors
You can also find the list here.

This post will tell you how to add colors to different things:

Add color to your prompt

Take a look at this example:

user@host$ ls cutlery/
butterknife.1  knife.3	    plate.dirty  spoon.4    teapot
butterknife.3  knife.4	    spoon.1	 tea.cup.1  tea.spoon.4
fork.3	       knife.5	    spoon.2	 tea.cup.2  tea.spoon.5
fork.7	       plate.clean  spoon.3	 tea.cup.3  tea.spoon.6
user@host$ clean_dishes -i cutlery/plate.dirty
This is a very useful program doing very useful things.
Version 3.2.1
Copyright 2016, by Marijn van Vliet

Parsing files...
Found a dirty plate! Looking for cleaning utensils:
ERROR:
        Could not find brush, see MANUAL for help on cutlery
user@host$ ls cutlery/
butterknife.1  knife.3	    plate.dirty  spoon.4    teapot
butterknife.3  knife.4	    spoon.1	 tea.cup.1  tea.spoon.4
fork.3	       knife.5	    spoon.2	 tea.cup.2  tea.spoon.5
fork.7	       plate.clean  spoon.3	 tea.cup.3  tea.spoon.6
user@host$ 

In the terminal session above, the user tried to execute the clean_dishes command. It failed with an error about not finding something, so the user investigated by checking what is in the cutlery/ folder. Notice how difficult it is to tell what is the input entered by the user, the output of the command and the directory listing.

With some configuration, the above session could look like this:

host:~> ls cutlery/
butterknife.1  knife.3	    plate.dirty  spoon.4    teapot
butterknife.3  knife.4	    spoon.1	 tea.cup.1  tea.spoon.4
fork.3	       knife.5	    spoon.2	 tea.cup.2  tea.spoon.5
fork.7	       plate.clean  spoon.3	 tea.cup.3  tea.spoon.6

host:~> clean_dishes -i cutlery/plate.dirty
This is a very useful program doing very useful things.
Version 3.2.1
Copyright 2016, by Marijn van Vliet

Parsing files...
Found a dirty plate! Looking for cleaning utensils:
ERROR:
        Could not find brush, see MANUAL for help on cutlery

host:~> ls cutlery/
butterknife.1  knife.3	    plate.dirty  spoon.4    teapot
butterknife.3  knife.4	    spoon.1	 tea.cup.1  tea.spoon.4
fork.3	       knife.5	    spoon.2	 tea.cup.2  tea.spoon.5
fork.7	       plate.clean  spoon.3	 tea.cup.3  tea.spoon.6

host:~>

Adding some color to the prompt (and a blank line) helps break up the wall of text, making each command→output block easy to identify.

The way to change your prompt depends on the shell you are using. Just type one of these commands in the terminal to play around with your prompt (if you don’t know which shell you are using, just try both commands):

for tcsh:

set prompt = "my prompt"

for bash:

export PS1 = "my prompt"

In the commands above, replace my prompt with whatever you want your prompt to be. You can also use special codes that add various things to your prompt, including colors. Here are my favorites:

Meaning tcsh bash
Current time %T \A
Current directory %~ \w
Hostname of machine %m \h
A blank line \n \n
The full list click here click here

For example, here is my tcsh prompt without color:

set prompt = "\n%T %m:%~> "

If you are using bash, this would be:

export PS1 = "\n\A \h:\w> "

Now let’s add some color! The codes required to display color are extremely cryptic:

Meaning tcsh bash
Set foreground color %{\033[38;5;###m%} \e[38;5;###m
Set background color %{\033[48;4;###m%} \e[48;4;###m
Reset color back to normal %{\033[0m%} \e[0m

In the above codes, replace ### with the number of the color you want (see the image above). At the end of your prompt, you want to put the “reset color” code, otherwise everything after your prompt will be colored as well. For example, here is my tcsh prompt with colors:

set prompt = "\n%{\033[38;5;202m%}%T %m:%~>%{\033[0m%} "

Making the change permanent

Up to now, we’ve been playing with the prompt interactively. To make your changes permanent, you add the command to set the prompt to a special file:

For tcsh: ~/.cshrc
For bash: ~/.bashrc

The above files contain a list of commands that get executed when you open a new terminal. Just add the command to set your favorite prompt to the list.

Make your prompt color depend on the host machine

One final tip for your prompt: if you use ssh to login to different machines, you can use color to help indicate on which machine you are at any time. This is particularly useful if you have multiple terminal windows, some of which are logged in to your local machine and some to the remote one. By setting distinctive colors, you can easily tell which is which when you switch between windows.

Normally, you can edit the ~/.cshrc or ~/.bashrc file on the remote machine to set a prompt with a certain color. However, in some cases, you have a “roaming” home directory, which means that the remote machine shares the ~/.cshrc and ~/.bashrc files with your local machine. In this case, you’ll need to use an if statement to select the prompt to use, based on the hostname of the machine. To find the hostname of the current machine, just type the suitably named hostname command in the terminal.

Here is an example for tcsh:

if (`hostname` == gyrus) then
    set prompt = "\n%{\033[38;5;202m%}%T %m:%~>%{\033[0m%} "
else if (`hostname` == dupond.ltl.hut.fi) then
    set prompt = "\n%{\033[38;5;021m%}%T %m:%~>%{\033[0m%} "
else if (`hostname` == cetaganda.ltl.hut.fi) then
    set prompt = "\n%{\033[38;5;088m%}%T %m:%~>%{\033[0m%} "
else
    set prompt = "\n%{\033[38;5;190m%}%T %m:%~>%{\033[0m%} "
endif

And here is an example for bash:

if [`hostname` = "gyrus"]; then
    PS1="\n\e[38;5;202m\A \h:\w>\e[0m "
else if [`hostname` = "dupond.ltl.hut.fi"]; then
    PS1="\n\e[38;5;021m\A \h:\w>\e[0m "
else if [`hostname` = "cetaganda.ltl.hut.fi"]; then
    PS1="\n\e[38;5;088m\A \h:\w>\e[0m "
else
    PS1="\n\e[38;5;190m\A \h:\w>\e[0m "
fi

Add color to your ls output

One of the commands you use most often is ls, which lists the files in the current folder. By default there are no colors in this list and if there are colors, they can be likely extremely ugly. The fact is that ls really needs some coloring to, for example, distinguish folders from normal files. For example, try to spot in the ls output below, which are the folders and which are the normal files:

13:37 host:~> ls
backup     giraffe                    Pictures
bin        gitprompt.csh              Public
cfg        kernel-30615.json          R
cutlery    lisp                       scikit_learn_data
data       login.keyring              SetUpFreeSurfer.csh
Desktop    matlab                     Steam
Documents  matlab_crash_dump.29703-1  Templates
Downloads  Music                      triton2
eeg.lout   pathdef.m                  Videos

Maybe you can with some detective work, but it is quite obvious that the following output is much clearer:

13:37 host:~> ls
backup     giraffe                    Pictures
bin        gitprompt.csh              Public
cfg        kernel-30615.json          R
cutlery    lisp                       scikit_learn_data
data       login.keyring              SetUpFreeSurfer.csh
Desktop    matlab                     Steam
Documents  matlab_crash_dump.29703-1  Templates
Downloads  Music                      triton2
eeg.lout   pathdef.m                  Videos

Folders are shown in light blue, links in dark blue, and regular files are shown in plain gray.

To make ls use colors, use the --color=auto switch like this:

ls --color=auto

Of course, this is a pain to type every time, so it’s best to set an alias for this. For tcsh, add the following line to your ~/.cshrc:

alias ls ls --color=auto

or for bash, add the following line to your ~/.bashrc:

alias ls="ls --color=auto"

Now to tweak the colors of the ls output. This is done with the dircolors program. First, make the program dump the current color settings to a file, like this:

dircolors -p > ~/.dircolors

This will create the file .dircolors in your home folder. Now edit this file with a text editor of your choice (i.e. nano ~/.dircolors). You will find it contains many lines that control the colors in detail. I will give you a breakdown of the most important things in there.

First, ignore all the lines that start with a # sign, they are comments and ignored by the program (you can use this to add little notes to yourself).
Next, there are a bunch of lines like:

TERM Eterm
TERM ansi
TERM color-xterm
TERM con132x25
...

Just leave them be.

Then, there are the most important lines:

#NORMAL 00 # no color code at all
#FILE 00 # regular file: use no color at all
RESET 0 # reset to "normal" color
DIR 01;34
LINK 01;36
MULTIHARDLINK 00
FIFO 40;33
SOCK 01;35
DOOR 01;35
BLK 40;33;01
CHR 40;33;01
ORPHAN 40;31;01
SETUID 37;41
SETGID 30;43
CAPABILITY 30;41
STICKY_OTHER_WRITABLE 30;42
OTHER_WRITABLE 34;42
STICKY 37;44
# This is for files with execute permission:
EXEC 01;32

These define the colors for the main file types. I recommend setting all of them to 00 (no color), except for folders and links, which you should give a nice distinctive color. Check the image at the top of this post for the color codes and set the DIR color for folders and the LINK color for links like this:

DIR 38;5;###
LINK 38;5;###

where you should replace ### with the number of the color you want (I use 20 and 33 for dark and light blue). You may also want to set EXEC to a color; these are files that are executable (i.e. programs). I set this to 00 on my machine, because I often find myself in shared folders where people have set every file to be executable (mounting folders hosted on a Windows machine will do that), even files that are not programs.

Finally, there are a lot of lines like this:

.tar 01;31
.tgz 01;31
.arj 01;31
.taz 01;31
.lzh 01;31
.lzma 01;31
.tlz 01;31
.txz 01;31
.zip 01;31
.z 01;31
.Z 01;31
.dz 01;31
.gz 01;31
...

These lines define file types. For example, all files with a name that ends in .tgz are colored red (01;31). You can remove all of these lines and instead add your own. One neat trick you can do is make temporary and other uninteresting files be gray so they fade into the background, for example those with names that end in .bak or .pyc,

To see the effect of your efforts, execute the following command for tcsh:

eval `dircolors -c ~/.dircolors`

or the following command for bash:

eval `dircolors -b ~/.dircolors`

Making the change permanent

To make the change in ls colors permanent, add the alias command and the dircolors command that you were typing in the terminal to your ~/.cshrc or ~/.bashrc file.

Add color to your git output

If you use git (and you should!), then you want to instruct it to use colors for things like the git diff command. Newer versions do this automatically, but for older versions you need to run this command once:

git config --global color.ui auto

For more tweaking of the colors, see this section of the git book.

Add color to your top output

The top command gives you a status report on what programs are running, how much CPU and memory they use, etc. Like Windows’ activity monitor, it’s very useful to see what is using a lot of resources and to terminate programs that misbehave. Unfortunately, this command does not use color.

But there is a replacement program that does! It’s called htop and can be installed (on Debian Linux variants) with a quick:

sudo apt-get install htop

It is a much more user friendly version of top and it looks a lot better too!1

Would you like to know more?

  • Here is a python script for converting hex colors to the nearest of the 256 colors available in the terminal.
  • Read more about the tcsh shell on the official website.
  • Read more about the bash shell in the manual.
  • Use Adobe’s Kular tool to come up with great color schemes.