When using zsh tab-completion, Ignore CDPATH if a local file or directory matches

autocompletezsh

I love Z shell's tab completion with CDPATH, but sometimes it's overkill. I'd like it to only kick in as a fall-back if nothing in the local directory matches.

For instance, I was just in ~/Code/Haxe and typed

$ cd h<tab>

knowing that the only directory there beginning with an h was haxe.org. Instead of completing the directory name I got a bunch of things beginning with h from my CDPATH. On the other hand, I wouldn't want to live without it, since (along with AUTO_CD) I can get to that directory (or countless others) simply by typing Haxe. I use this all the time, but I'd like to make it take a backseat to switching to local directories.

I've not found any questions that deal with this problem. This one deals with the display of suggested completions, but that's not what I care about. I want to simply ignore everything from CDPATH when a local directory matches perfectly.

Best Answer

There isn't any zstyle setting for that. What you can do is try completion with an empty cdpath, and if that fails, try with cdpath.

_cd_try_without_cdpath () {
  CDPATH= _cd "$@" || _cd "$@"
}
compdef _cd_try_without_cdpath cd pushd