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?



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'

group names

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.

Note 1: zsh has two representations for array variables like PATH and CDPATH, of which the lower case variant is an actual array. This means you can:

cdpath=(path/to/dir /path/to/another/dir)

Or, to get your desired result:

cdpath=(.. ../..)

I personally find it a bit more readable than the colon-separated pseudo array.

Note 2: Exporting CDPATH isn't necessary unless you have several programs that want it set.

  • 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! – PonyEars May 22 '11 at 7:40
  • I was having a hard time with cd completion until I made these settings. Thanks. – ismail Mar 10 '13 at 5:50

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.