Search code examples
zshzsh-completion

Custom zsh completion: custom sort?


I'm using _values to add completion to a custom zsh command, but unfortunately, zsh ignores the order in which I pass the values and sorts in reverse order of what I what (I want sort -rn, basically).

Is there anyway to force it to respect the order I've passed, or if not, to somehow modify the sorting mechanism used for that specific command (i.e., I don't want to change the order of all completions, just this specific one.

Example:

_values 'my completions' foo bar

shows bar before foo, but I want foo before bar.


Solution

  • In general, Zsh completion matches can be added in either a sorted or unsorted group. However, _values does not include an option to specify this. So you can do:

    _wanted -V values expl 'my completions' compadd foo bar
    

    Which features of _values do you want? If you want a delimited list with no duplicates, _sequence can wrap the above. If you want per-match descriptions, _describe can help. But if you want some of the more complex features of _values, you're either stuck with the sorting or you have to do a lot more manually.