2

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.

Gal
  • 5,338
  • 5
  • 33
  • 55

1 Answers1

4

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.

okapi
  • 1,340
  • 9
  • 17
  • 1
    I've tried using `_describe`, but when I added the `-V` option to it, it doesn't seem to work. My code is [here](https://pastebin.com/dcaHYpga). When I try to complete `hello`, it isn't ordered but the description of the match group is changed to `-V`. If I replace the 'stuff' string with, say, 'my desc', it only completes the first entry in the array, and it includes the description as part of the completion, i.e., `hello foo:foo\ desc`, which is all sorts of odd. – Gal Feb 07 '18 at 09:40
  • 1
    Your code works for me in zsh 5.4.2 (and also 5.2) but not in 5.0.2. Perhaps try a newer zsh release? – okapi Feb 08 '18 at 12:29
  • Unfortunately, my work station is limited to 5.0.5 :( Thanks for verifying the root cause though! – Gal Feb 11 '18 at 08:14
  • I stumbled over this question while researching why my `git checkout` recent branches completion was sorted the wrong way (lexical instead of MRO). They use `_describe -V` internally. The fix was to turn off `list-grouped` for the tag[1]. I guess the grouping will mess up the order (sorta makes sense). [1] `zstyle ':completion:*:*:git-checkout:*:recent-branches' list-grouped false` – olejorgenb Jul 12 '22 at 06:10