I sometimes find having the full directory path in the prompt to be overwhelming, so I've created this small bash function which shows just the last two parts of the path:
short_dir () { if [ "$PWD" = "$HOME" ]; then echo '~' else the_dir=`dirname "$PWD"`; the_base=`basename "$PWD"`; the_dir_base=`basename "$the_dir"`; if [ "$the_dir_base" = '/' ]; then the_dir_base=""; fi echo "$the_dir_base/$the_base" fi }Add a call to this to your prompt, don't forget to escape the backquotes:
export PS1="\`short_dir\`\$ "
So, if your current directory is /usr/local/man/man1, your prompt would look like:
man/man1$
You can, of course, sprinkle in other bash prompt special characters, such as "\h" for the hostname:
export PS1="(\h) \`short_dir\`\$ "
produces:
(myhost) man/man1$
Tags:
2 comments:
Sorry, does your example contain a typo? Should not it be:
export PS1="\`short_dir\`\$ "
(and/or the funcion be called "myprompt" instead of "short_dir")
Ah, good catch. I've edited the post to fix the example.
As I was preparing this, I refactored a little. Previously the function was called "myprompt" and generated the full string of the prompt. I decided that I should let bash natively handle what it already handled well and my function should just generate the shorter path... and I changed the name to reflect this... but I didn't refactor my examples.
Post a Comment