November 22, 2004

CLI Magic: Colorize your prompt

Author: Joe Barr

Last week's column on prompt mods resulted in reader comments showing colorized prompts and a request for a similar column dealing exclusively with colorizing. This is much easier to do than looking at the examples might suggest. It's all a matter of shutting down your brain so that it doesn't try to parse the command into human-readable text. But don't be afraid about shutting down the brain. If you're stuck in a GUI, that happened long ago. Now walk this way.It's like reading "Omeros" by Derek Walcott for the first time. Sometimes the imagery is just too much to cope with. We're talking about really arcane stuff, escape codes and color codes, and sandwiching terminal commands into our Bash command. But it really is easy to do, once you get your brain rewired.

Let's take a look first at a plain-Jane prompt of the sort we described last week, then then compare it with a similar prompt -- which has been colorized. We'll want our plain-Jane prompt to move to a new line, then display username@host and a shortened PWD. To get that, we enter:

export PS1='\n \u@\h \W'

Which results in a prompt that looks like this:

warthawg@linux downloads

The gobbledygook used in setting that prompt display may hurt your eyes at first, but it breaks down very simply:

  • \n Is the newline function
  • \u@ Specifies the user name followed by the "@" sign
  • \h Specifies the host name
  • \W Specifies the shortened version of the current directory

Now let's say we want the current directory to stand out a little bit more by coloring it green. To do so, all we need to do is add the X11 terminal control code around the Bash code in the prompt command above which results in the directory being shown.

If you haven't already escaped from this madness, you may be asking yourself, how in the world do I do that? The answer, grasshopper, is that you must escape. We do that by inserting an escape code into the prompt command, add a code to specify the color you want to use, and then terminate the escape with another code.

We begin the makeover by inserting the escape code in front of the directory command in the prompt, like this:

PS1='\n \u@\h \e[ \W'

Color Foreground Background
Black 30 40
Red 31 41
Green 32 42
Yellow 33 43
Blue 34 44
Magenta 35 45
Cyan 36 46
White 37 47

But don't export that new PS1 just yet, we have some color painting to do first. We can set both the foreground and the background colors in the prompt by specifying -- what else? -- a numeric code for one of our eight color choices. Just using the same code for the same color in either context -- background or foreground -- would be too easy. So there are different codes for the same color, depending on that context. The table lists all of the color codes available to us:

Now, back to our prompt under construction. Let's show the current directory in green text on the current background. To do that, we need to add the color codes behind the escape code, so it looks like this:

PS1='\n \u@\h \e[32m \W'

The 32 specifies green foreground, the 47 white background, and the m signals the end of the color specifications. But we still have work to do. We need to end the prompt with a command to revert to the default color scheme, otherwise everything that appears after the prompt will be colored as well. To signal the end of the coloring, all we need to do is escape again and use zero for the color code, like this:

export PS1='\n \u@\h \e[32m \W \e[0m'

Now export your shiny, new, and (hopefully) colorful prompt and see how you like it. If you don't, refer to the handy table above and repeat with different colors until you do.

Click Here!