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.

  • Is the first char :? – mikeserv Dec 20 '14 at 3:36

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
  • 2
    zstyle ':completion:*:complete:(cd|pushd):*' tag-order 'local-directories named-directories' seems to be a cleaner way to acheive this. It excludes path-directories from the tags that will be generated, but they'll still be used as a fallback if no other results are generated. – wincent Nov 26 '18 at 22:28

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.