A little bit of bash prompt hackery
I like to have my shell prompt read like so:
[user@host: /where/i/am]$
I also like my xterm or PuTTY title bar to read the same way, only without the square brackets. That way when I’m cycling through a ton of different windows, it’s easy to tell which one I need. To get this effect I typically set my PS1 variable to [\u@\h: \w]\$ and PROMPT_COMMAND to ’echo -e "\\033]0;${USER}@${HOSTNAME}: ${PWD}"\\007\\c’. In PS1, the \w denotes the full pathname of the present working directory, calculated on the fly, so the title bar is always updated when I change directories.
That’s great as long as I keep mostly to my home directory. When I’m coding in a deeply nested directory, though, the length of the shell prompt gets out of hand quickly. Anybody who keeps working copies of source-controlled code will know what I mean here. I used to change PS1 in these cases to use \W (capital W), which denotes just the basename of the present working directory, but I have never been satisfied with having to do this manually. Using two aliases and some PROMPT_COMMAND magic seems to get me the best of both worlds:
alias sp="export PS1='[\u@\h: \W]\$ '" #for "short path"
alias lp="export PS1='[\u@\h: \w]\$ '" # for "long path"
export PROMPT_COMMAND='echo -e "\\033]0;${USER}@${HOSTNAME}: ${PWD}"\\007\\c;
if [ -d ${PWD}/.svn -o -d ${PWD}/CVS -o `echo $PWD | wc -m` -gt 30 ];
then sp; else lp; fi'
With these settings in effect, I get a long prompt unless either
- The full pathname of the current directory exceeds 30 characters in length
- The current directory contains a directory called .svn or CVS
I think the ideal would be to have the full pathname truncated to some length (say 20 characters), but truncated toward the start of the string rather than the end and the truncated bit replaced with an ellipsis, so the prompt might read [user@host: ...rc/foo/bar/baz/myApp]$ , but I see no way to do this without major contortions.