Take the 2-minute tour ×
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.

I have my prompt set to the following:

PROMPT=%F{reset}[%F{blue}%2/%F{reset}]

which displays two parent directories as

[foo/bar]

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

[~]
[~/foo]

respectively, instead of

[/Users/me]
[me/foo]

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
add comment

1 Answer 1

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
~l$ 

(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
add comment

Your Answer

 
discard

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