October 27, 2008

Colour Prompt

Filed under: Uncategorized — Tags: , , , , , , , , — alsuren @ 9:25 pm

Inspired by konversation’s pretty colours for users in an IRC channel, and pissed off with forgetting which of the many (smaug, soup, pip, concorde, harrier, excalibur, telford, dl325) linux boxes I’m remotely logged into at any given time, I decided to assign a different prompt to each host. Since some of the machines share home directories but not installed software, hard-coding a colour into each machine’s ~/.bashrc wouldn’t hack it.

That’s enough talking… I present to you “colourhash”.

# Save this file as 'colourhash', and put it somewhere in your $PATH
# Usage: colourhash [string] ; echo message
# Wherever you see a colour escape sequence in your ~/.bashrc (like 33[01;32m (bold green))
# Replace it with '$(colourhash)'
if [[ -n $@ ]]
# We have command line arguments
# We don't have command line arguments. Create a colour for this user@host combo
echo -n ${string} | cksum | (read hash tail
# We only really care about the first word printed by cksum: a crc hash of $string.
# Also note that crc is not a cryptographic checksum. This is not important, since we
# only have 7 colours to pick from, so hash collisions will be frequent.
# The readable colour codes lie between 31 and 37
colour_code=$(( 31 + ($hash % 7) ))
# Magic escape sequence follows:
echo -en '33[01;'${colour_code}'m'

Right now I’m only picking one of 7 colours. Who fancies doing the birthday problem on that?

If I end up getting too many hosts the same colour, I will start thinking of ways to increase the number of colours available. There are potentially lots of colour combinations to pick from, but changing the background colour might look be a bit odd. If anyone wants to wrap this up and push it into ubuntu/gentoo, give me a shout.

This is a good reference for anyone adding colours to shell programs. Code to generate the above grid can be found here.



  1. A few ideas:
    * You could always include the hostname in your bash prompt: PS1=’\h$’.
    * You could have a case block in .bashrc selecting on the value of $HOSTNAME, and hardcode which colour you want to use for each host.
    * Using specific escape sequences might not be portable. As long as it works there’s no need to worry; otherwise you might have to say PS1=`tput …` or something. In that case you might have to read the long terminfo manpage.

    Comment by Gavin S. — November 12, 2008 @ 4:46 pm

  2. I already have my hostname in my prompt, but when do you ever read your prompt? Having a colour or picture for your prompt is definitely the way forwards.

    I’m actually starting to think that I might start using ipython as my shell, since it’s made entirely of win, and I could feasibly add a little python completer that hooks into zsh or bash for tab completions, and also some kind of man-page-parsing intelligent completer for command line args (or at least on debian based systems with an “every executable must have a man-page” policy).

    I will post about it soon-ish.

    Comment by alsuren — November 13, 2008 @ 4:58 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at

%d bloggers like this: