Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 3 down vote accepted

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).

share|improve this answer
Oh dear... so simple. Thanks! :) – user34059 Mar 12 '13 at 17:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.