Linux.com

Feature

CLI Magic: Colorize your prompt

By Joe Barr on November 22, 2004 (8:00:00 AM)

Share    Print    Comments   

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.

Share    Print    Comments   

Comments

on CLI Magic: Colorize your prompt

Note: Comments are owned by the poster. We are not responsible for their content.

Check your HTML!

Posted by: Anonymous Coward on November 25, 2004 06:35 AM
You should check your HTML source code.

Your magic doestn work, because every sane browser renders your escapes partially as empty.

Copying your examples from the browser window doesnt work for anybody.

#

Re:Check your HTML!

Posted by: Joe Barr on November 25, 2004 12:16 PM

That's strange. I copied the last prompt in the story into my browser and pasted it into a terminal. It worked just fine.


Joe Barr

#

Thank you

Posted by: Anonymous Coward on November 29, 2004 01:18 AM
many thanks for this helpful article, i've added these commands to my<nobr> <wbr></nobr>.bashrc and now i'm linuxing in full technicolor.

rgs
eric

#

Bash prompting examples

Posted by: Anonymous Coward on November 24, 2004 08:56 PM
If you are wondering just how far this can be taken, take a look at <A HREF="http://www.dreaming.org/~giles/bashprompt/prompts/index.html" title="dreaming.org"> Bash prompt examples</a dreaming.org> for a number of far more complex examples. And there is also the <A HREF="http://www.dreaming.org/~giles/bashprompt/howto/book1.html" title="dreaming.org"> Bash Prompt HOWTO.</a dreaming.org>

Cheers,
Toby Haynes

#

Use a script to make it even easier

Posted by: Anonymous Coward on November 25, 2004 12:11 AM
I built one and put it here
<A HREF="http://wikiunix.org/phpwiki/index.php/prompt" title="wikiunix.org">http://wikiunix.org/phpwiki/index.php/prompt</a wikiunix.org>

#

Using TPUT

Posted by: Anonymous Coward on November 25, 2004 01:44 AM
I suggest using the tput command, rather than ANSI. It recognizes your terminal, and outputs the correct escape codes for whatever terminal you are using (eg. VT102, etc.). As well, if your terminal doesn't support a particular attribute, it doesn't output anything.

There are a bunch of examples, eg.
http://www.linuxforum.com/linux-bash/clockt.html
http://www.patoche.org/LTT/shell/00000080.html
http://lists.debian.org/debian-user/1998/12/msg00<nobr>3<wbr></nobr> 23.html

Cheers,
Brian Hunt

#

Re:Using TPUT

Posted by: Joe Barr on November 25, 2004 04:36 AM
Thanks, Brian!


Joe Barr

#

You need to include ANSI escapes

Posted by: Anonymous Coward on July 27, 2006 10:40 PM
You need to include ANSI escapes like so \[ --STUFF-- \] around these or some terminals will give you line wrap problems.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya