Prompt Insanity

Note: This entry has been restored from old archives.

My $PS1 is a creature evolved over the years, it amazes me to consider how much accumulated time I must have spent on this simple little critter. For the last couple of years it’s been pretty stable as the simple “u@h:W$” with some occasional colour. I’ve been through phases of executing commands in my $PS1 to add further data (don’t, it fucks with $?) and I have a custom $PROMPT_COMMAND to issue a terminal control sequence to put similar info in titlebars (where I execute a $(date …) to add a timestamp, this doesn’t bother $? though).

Yesterday I noticed a recurring problem… I keep loosing a $? that I’m interested in. Often because I go to another terminal and do something else while something executes, then I return and do something silly like “automatic ls“. Does anyone else do “automatic ls” I wonder, it’s where you run ls for no particular reason — I tend to do it every time I switch to another terminal, also if I’m just looking at an existing terminal and thinking my fingers seem to have a “background ls” function. It’s like looking around the room I guess.

Anyway, back onto the point. This little problem prompted me to redo my prompt and along the way find a better way to do the terminal title:

    ps1xt='[33[1;35m]$?[33[0m]' # Last exit code, magenta
    ps1us='[33[1;32m]u[33[0m]' # Current username, green
    ps1mc='[33[1;36m]h[33[0m]' # Current hostname, cyan
    ps1wd='[33[0;32m]w[33[0m]' # Current working dir (w is full, W is basename), dark green
    export PS1="[33]0;<$?>u@h:w(t)07]<$ps1xt>$ps1us@$ps1mc:$ps1wd$"

So, no more $PROMPT_COMMAND as the control sequence is embedded in $PS1 now (everything within the first […] group). This gives an added bonus of being able to simply use $PS1 substitutions for all the info. The only real difference between this prompt and what I had before is the addition of the <$?> at the start, so now my exit codes will always been in the terminal backlog! (Until I do something else automatic like a ^L.) Also added the $? to the title bar, which works well since I can see the code when I’m in another window frame this way.

One possible annoyance is the extra length of the prompt, the last thing you want is a prompt taking up overt character width. Two possible reductions would be to” 1) remove the u part, how often are you not sure who you are? 2) Make w a W so the CWD part can’t get too long so easily. Given the colour differences you could also remove the punctuation I guess, save yourself 4 chars. You could also stick a n in before the final $ and make a two-line prompt I guess, I used to have one like that (a long time ago when I first discovered PS1 and put everything but the kitchen sink in there).

A lot of people will hate the colourfulness, I hate it myself sometimes. The main function is that I use different colours for the h on different machines so I can very quickly recognise which machine I’m looking at. The rest of the colour is just for the sake of being garish.

<0>yseth@odysseus:~$ls -e
ls: invalid option -- e
Try `ls --help' for more information.
<2>yseth@odysseus:~$ls -e

“:;”? OR Re: Re: Prompt Insanity

Really, one of these days I might do something about comments.

Sometimes I do get upset at my long and garish prompt and blast it with PS1=’>’.

I hit the good old ‘arg list too long’ fairly often, dealing with very large HTTP corpora. I’m rarely in a corpus directory though so this has yet to break my auto-ls habit.

As for other shells, I’m not diverse enough in my shell usage to consider the world outside of bash. I would have thought that .bashrc’s PS1 setting wouldn’t matter there.

Finally, I pretty soon realised that embedding my usual PROMPT_COMMAND into PS1 has some occasional issues – it isn’t “magically invisible” when you have a console login … so now I have made it conditional in a $TERM case…esac
block.

For the “good” of the web:

    ps1xt='[33[1;35m]$?[33[0m]' # Last exit code, magenta
    ps1us='[33[1;32m]u[33[0m]' # Current username, green
    ps1mc='[33[1;36m]h[33[0m]' # Current hostname, cyan
    ps1wd='[33[0;32m]w[33[0m]' # Current working dir (w is full, W is basename), dark green
    case x$TERM in
    xxterm|xrxvt)
        ps1pc='[33]0;$?:u@h:w(t)07]'
        ;;
    esac
    export PS1="$ps1pc$ps1xt:$ps1us@$ps1mc:$ps1wd$"