Lately I’ve fiddled a lot with installing virtuoso on some virtual machines and found myself repeatedly asking bash for the return value of the last command echo $?
. I remembered this blog post by Gecko quite a while ago and tuned it a bit to my needs. My user prompt now looks like this (it’s a slightly modified version of the old gentoo defaults, that I prefer over the ubuntu defaults, which only remind you that you’re root with a # instead of a $):
The code for the user prompt:
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m]u@h\[\033[00m]:\[\033[01;34m]w $(if [[ $? = 0 ]];then echo "\[\033[01;34m]";else echo "\[\033[01;31m]";fi)$ \[\033[00m]'
And the root prompt:
PS1='\[\033[01;31m]h\[\033[01;34m] W $(if [[ $? = 0 ]];then echo "\[\033[01;34m]";else echo "\[\033[01;31m]";fi)$\[\033[00m] '
And a small doc snippet you might want to include with your PS1 in your .bashrc, so you can still understand that cryptic stuff in a few days:
# h hostname
# u user
# w working dir (home == ~)
# $ a $ if UID == 0 else #
# A current 24-time: HH:MM
# \ a
# [ begin, ] end of non-printing (control chars)
#
# colors: have to be surrounded by: '\[\033[' and 'm]' (without the ''. These literally are the left and right bracket!)
# color codes are as follows, preceeded by a '0;' (dark) (default) or a '1;' (light)
# FG and BG NULL: 00 resets
# FG: 30 + ... 0: black, 1: red, 2: green, 3: yellow, 4: blue, 5: violet, 6: cyan, 7: white
# BG: 40 + ... same
If it’s getting more complicated, the PROMPT_COMMAND environment variable can be of great help:
function promptcmd() {
# ...
PS1=u@h:$GITBRANCH$SVNWORKINGCOPY...:w$
}
export PROMPT_COMMAND=promptcmd
I learned this from Frank (http://amazing-development.com/) in the comments to http://gnoack.wordpress.com/2009/10/06/svn-bash-prompts/ , have a look at these for a more elaborate example.
Another useful hint, I was first unaware of: Occasionally, when playing with colors, my bash prompts got messed up. This is the readline library not being able to count the number of visible characters in your PS1, they need to be escaped using [, ].
Did you know readline allows you to use all these useful Emacs shortcuts? 🙂 M-f, M-b for moving back and forth word-by-word, C-f, C-b, C-p, C-n, C-d to delete characters, M-d to delete the next word. It even has a kill-ring! 🙂 (There is also a vi mode.) This is my discovery of the last few weeks, I’m a big fan! 🙂
Nice, indeed 😉