I have my prompt set to the following:


which displays two parent directories as


However, when I'm in $HOME or one directory deep in $HOME, I'd like the prompt to be displayed as


respectively, instead of


I've tried approaches like Steve Losh's collapse_pwd (and the simpler version: ${PWD/#$HOME/~} also mentioned in the article) and other similar ones, but I lose the flexibility of being able to show only the last two directories.

How can I modify the above to handle $HOME? My zsh version is zsh 5.0.0 (x86_64-apple-darwin12.0.0) on OS X 10.8.2

1 Answer 1


zsh has built-in support for that. Use %2~ instead of %2/. See also %40<...<%~

See also the autonamedirs option:

$ l=/usr/local
$ PS1='%~$ '
~$ setopt autonamedirs
~$ cd /usr/local

(without autonamedirs, you can still do cd ~l to cd to /usr/local and your prompt to show ~l).


You must log in to answer this question.