Fish-like argument completion search in ZSH

autocompletezsh

I was messing around with fish and noticed this handy behavior

If I typed wget -<tab><tab><tab>, I was put into an interactive menu. However, when I typed, I searched the descriptions of the arguments themselves. I tried this in zsh, and typing in this menu only seemed to bring me back to my interactive prompt. Is there a way to achieve similar functionality in zsh?

Best Answer

Try to put this in your .zshrc file:

 # load module for list-style selection
 zmodload zsh/complist

 # use the module above for autocomplete selection
 zstyle ':completion:*' menu yes select

 # now we can define keybindings for complist module
 # you want to trigger search on autocomplete items
 # so we'll bind some key to trigger history-incremental-search-forward function
 bindkey -M menuselect '?' history-incremental-search-forward

Now if you type wget -<tab> menu with autocompletions appears. We defined keybind for this menu, so if you press ? search line appears on the top of the completions:

davidsykora~%wget --referer
isearch: refe

Some additional tips:

  • you can also define bindkey -M menuselect '/' history-incremental-search-backward for backward search
  • if you press ? during a search again selection will jump to the next search result. Use / for jump to previous result
  • it takes a lot of tweaking to make it works as you want so take a look at complist documentation to find additional functions and options
  • for me is usually much quicker to use vi-insert complist function - it filters completions based on what you type.
Related Question