I have the following in my .bash_profile (from a similar question here:

PROMPT_COMMAND='pwd2=$(sed "s:\([^/]\)[^/]*/:\1/:g" <<<$PWD)'
PS1='\u@\h:$pwd2\$ '

However, if the current directory is within a .dir (such as ~/.vim/bundle/) then the prompt just displays a .:


I would like it instead to retain 1 char for all dirnames unless it has a dot, in which case it would show two, like this:


Even better would be if I also have the home directory represented by a ~ like this:


Any ideas?

  • Is it OK just putting the basename of the current working directory in the prompting instead of the whole directory, and use pwd command if you want to get the whole directory? That means use \W instead \w. This solution is simple and work for me.
    – Edw4rd
    Nov 16, 2012 at 20:09

1 Answer 1


This seems to do the trick, adding an optional . to the capture:

PROMPT_COMMAND='pwd2=$(sed "s:\(\.\?[^/]\)[^/]*/:\1/:g" <<<$PWD)'
PS1='\u@\h:$pwd2\$ '

And for the 'even better':

PROMPT_COMMAND='pwd2=$(sed -e "s:$HOME:~:" -e "s:\(\.\?[^/]\)[^/]*/:\1/:g" <<<$PWD)'
PS1='\u@\h:$pwd2\$ '
  • hmmm...neither of these work on my system: the prompt shows the full dirnames in the path ... chris@DeathStar:/Users/chris/db/personal$
    – Chris
    Nov 16, 2012 at 20:38
  • Hmm. Could be an incompatibility between gnu sed and osx sed. Don't have an osx machine to test on. Nov 16, 2012 at 20:40
  • Thank you! That pointed me in the right direction... installed gnu-sed and all it works!
    – Chris
    Nov 16, 2012 at 23:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.