My CDPATH in zsh looks like this:

export CDPATH='.:..:../..'

Unfortunately, this means that when I try to autocomplete after typing 'cd ', there're way too many irrelevant autocomplete options, depending on where I am. For example, if I'm in my home directory, typing cd tries to autocomplete every user on the system because CDPATH has ".."

Is there a way to keep my CDPATH as shown above, but have the autocomplete for cd, pushd, etc. ignore the CDPATH value, and autocomplete only based on the current directory?

link|improve this question
feedback

1 Answer

up vote 3 down vote accepted

One way is to add the following completion settings in your .zshrc to remove path-directories from the suggestion sources.

zstyle ':completion:*:complete:(cd|pushd):*' tag-order \
    'local-directories named-directories'

Alternatively or additionally, the following settings should display a heading for all respective groups of completion suggestions so you can see which directories are local directories and which are suggestions from your cdpath.

zstyle ':completion:*' group-name ''
zstyle ':completion:*:descriptions' format %d

You can apply standard prompt formats to these descriptions to make them stand out:

zstyle ':completion:*:descriptions' format %B%d%b        # bold
# zstyle ':completion:*:descriptions' format %S%d%s        # invert/standout
# zstyle ':completion:*:descriptions' format %U%d%u        # underline
# zstyle ':completion:*:descriptions' format %F{green}%d%f # green foreground
# zstyle ':completion:*:descriptions' format %K{blue}%d%k  # blue background
# etc.

That helps make sense of the different sources quite a bit in my experience.


By the way, zsh has two representations for those array variables like PATH and CDPATH, of which the lower case variant is a true array. This means you can:

cdpath=(.. ../..)
export CDPATH

I personally find it a bit more readable.

link|improve this answer
1  
This is great. Thanks not only for giving me exactly the answer I was looking for, but also pointing me to very useful things like grouping and formatting (and even the true-array tip) I wouldn't have even thought of exploring! – redstreet May 22 '11 at 7:40
feedback

Your Answer

 
or
required, but never shown

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