Skip to main content
Tweeted twitter.com/StackUnix/status/1199070398723379200
Source Link
artfulrobot
  • 3.1k
  • 7
  • 33
  • 41

How to provide autocomplete suggestions that don't match on prefix in zsh completion

I have a zsh auto completion function for a command called gd which does a fuzzy search based on the $words[CURRENT] value and return suggestions. The search is working fine but zsh does not show any candidates because the entered text is not a prefix of the suggestion.

e.g.

% gd hw 

Might generate the completion oh hello there, world

But zsh won't show that because hw is not found at the start of the suggestion.

Can I make zsh offer the suggestion as something that would replace the current word when chosen?

Here's a simplified version of my completion script in a file called _gd which is in my fpath:

#compdef gd _gd() { local -a list clue="$words[CURRENT]" pattern="" for i in $(seq $#clue) do pattern="$pattern.*${clue[i]}" done while read dir do list=( $list $dir ) done <<<$(grep "$pattern" ~/.recent-dirs) _describe gd list }