I would like nm[TAB] to expand to node_modules in zsh. Is this possible? How can it be done?

Ideally, it wouldn't be triggered if there is another completion available, such as an already existing nm directory, and only trigger if there is a node_modules folder that exists, and complete if it is a part of a path, such as cd nm/mo[TAB] -> cd node_modules/moment.


1 Answer 1


Short & sweet answer

Try my zsh-autocomplete plugin. It configures Zsh's completion system to do what you're asking for out of the box, plus lots of other useful features.

Longer, DIY answer

To get fuzzy completion in Zsh, add this to your .zshrc:

zstyle ':completion:*' matcher-list 'r:|?=**'

What this does is tell the completion system to allow any number of characters added anywhere to the string you've typed.

Next, to get the thing you want with directories, add this:

zstyle ':completion:*' accept-exact-dirs true
zstyle ':completion:*' list-suffixes true
  • accept-exact-dirs makes it so that when you have two dirs nm and node_modules and you type nm, it will never try to complete it to the latter.
  • list-suffixes tells it to try to complete every partial directory name in the path you've entered and not just the first one.

To get the accept-exact-dirs-style behavior also for all other completions, add this:

zstyle ':completion:*' accept-exact true

More info can be found here: http://zsh.sourceforge.net/Doc/Release/Completion-System.html#Standard-Styles

  • Asked 6 years ago and answered beautifully! thanks! Unfortunately, your plugin has some significant jank on my setup, so i disabled it for now. I'll comment in your repo.
    – forivall
    May 28, 2020 at 18:02
  • Thanks! Looking forward to your comments. 🙂 May 31, 2020 at 9:06
  • For the matcher-list style, 'l:?||_=*' is perhaps closer to what the question actually asks for. Assuming the underscore is to be taken as significant in the completion trial candidates.
    – okapi
    Jun 4, 2020 at 21:43

You must log in to answer this question.

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