4

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.

1
  • Is the first char :?
    – mikeserv
    Dec 20, 2014 at 3:36

2 Answers 2

3

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
1
  • 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. Nov 26, 2018 at 22:28
0

I found this question because my console switched to a folder in ~ (which is in my CDPATH) instead of a subfolder of where I currently was. I'm not sure if this is exactly what this question is about, but my solution was to simply append .: at the start of CDPATH. So then the entry in .bashrc looked like this:

CDPATH=".:/home/fabian:$drive"

That way cd always prefers whatever is in the current folder, but can still switch to the others if there's nothing in the current one.

(Also, I was using Bash instead of ZSH, but it's likely the same or similar for both.)

Note: This breaks "cd" (without arguments) as a shortcut for "cd ~".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .