Now I have a colourful `bash` prompt
After seeing Adriano Castro’s presentation last week on R3, I was inspired both to play a little bit with R3 itself (cool!), and just as importantly, to finally taking a few minutes to customize my bash prompt. His brightly coloured prompt was full of information and life, mine not; I’d just done an entire presentation on loving the terminal, so this deficit was particularly shameful.
So, I spent a few minutes this evening toying around with things, and ended up with this:
Ah, lovely; it’s really simple to do for yourself.
Here’s how it works
The terminal prompt is controlled via the
PS1 environment variable in
bash. You simply need to construct a particular string, and assign it to that variable by adding a line to your
.bash_login file like the following:
export PS1="<your formatting string goes here>"
This prompt in particular is:
export PS1="\[\033]2;\u@\h\a[\[\033[37;44;1m\]\t\[\033[0m\]] \[\033[32m\]\w\[\033[0m\] \$ \[\033[0m\]"
This breaks down into:
\[\033]2;\u@\h\a # which writes the `user@host` string # into the terminal window's title bar [\[\033[37;44;1m\]\t\[\033[0m\]] # which writes (in white-on-blue) # `[HH:MM:SS]` at the beginning of # each line, so that I know exactly # when I executed a command \[\033[32m\]\w\[\033[0m\] # which writes (in a pleasant green) # the current working directory \$ # which writes "$" if I'm logged in as # a normal user, and "#" if I'm logged # in as `root`.
To build your own, I’d suggest taking a look at Daniel Robbins’ “Prompt Magic” article on IBM developerWorks. It’s a well put-together article that walks you through the whole, terrifically geeky process.
If you happen to have some strange text-wrapping problems, I might have a solution for you. In a nutshell, end your prompt with a colour code, and use
\033 instead of
\e. I’ve updated this page accordingly; see “Solving strange text wrapping problems in bash” for more details.