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?

  • Ive wanted to know how to do this for a long time. When it comes to standard keyword completion (not options) vim does it by allowing you to configure completion to keep focus on the line buffer, that way, if the completion list is too long, you just continue typing and the list naturally filters down. But zsh immediately switches focus to the menu-selection widget – the_velour_fog May 28 '17 at 2:43
  • I don't understand the behavior you're describing. Is this about searching the descriptions of the options rather than the option names? Or is this about the way to navigate the choices? What menu are you referring to (zsh has a lot of different settings for completion)? – Gilles 'SO- stop being evil' May 28 '17 at 22:03
  • @Gilles Yeah, you can use it to search the descriptions of options, or even the option names themselves. The menu I'm referring to appears when you type a command, a dash, then press tab twice. For instance, typing ls -<tab><tab> enters me into a menu where I can scroll up and down the different options. In fish, I'm able to type, which will then search these options and their descriptions. I'm hoping to achieve similar functionality in zsh. – ollien May 29 '17 at 0:56
  • I'm still unsure what you mean exactly. Is it the interactivity that you want? i.e. being able to select from the list with arrows and highlighting options? You say In fish, I'm able to type, which will then search these options and their descriptions. You can "search these options and their descriptions" in zsh. e.g. ls --h<tab> searches for the options beginning with --h and lists four matches. – Sparhawk Jun 5 '17 at 6:19
  • Take a look at this screenshot. If I type ls -size<tab> I get ls --block-size and ls --size as options in zsh. However, fish recommends me --size, --tabsize, -k, -S, --block-size, and --human-readable. I was hoping to achieve similar functionality in zsh. Not only does it search the names of commands, it also searches the descriptions – ollien Jun 5 '17 at 12:48

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.

I think this does what you want:

  zmodload zsh/complist
  setopt menucomplete
  zstyle ':completion:*' menu select=0 search

This will cause the list to show up when you hit tab, and then if you continue typing it will search the available options, including descriptions, with an incremental search.

You may also be interested in the same configuration, but with interactive instead of search. In that case as you type it will filter down the completions to just the ones that match what you have typed so far.

  • Hmm... if I type wget -<tab>no this doesn't filter down the completion list. This just continues typing 'no' into the terminal after --accept-regex=, which is the first option in the completion list. – ollien Jan 25 '18 at 4:15
  • 1
    huh, that isn't what happens for me. But it doesn't actually fiter with the search option, it just does an incremental search through the possibilites – Thayne Jan 25 '18 at 17:24

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.