1

I have zsh completion rule for custom script. My script can take zero or more options (starting with -) and zero or more arbitrary strings (patterns):

_arguments -S : \
    '--foo' \
    '--bar' \
    '1:pattern:'

this rule works only partially, in following format:

script "somestring" <TAB>

offers --foo or --bar and --f<TAB> automatically completes --foo

However,

script <TAB>

does nothing, where I would like to have options listed (--foo , --bar)

and even when I explicitly start with -- it shows the options but does not autocomplete them even when I am typing --f<TAB> and there is no ambiguity what option I want.

script --f<TAB>

should automatically completete --foo

So, to sum up, since string can be anything, there is nothing to autocomplete. Therefore, when I press , I want to autocomplete the --options, regardless whether i have alredy typed a string/pattern or not

I guess, I have to somehow specify in my completion rule that anything starting with - is an option, not string, and therefore should be auto completed from available options

1 Answer 1

1

I have found something which works with me:

___script() { _arguments -S : }
__script() { _arguments -S : --foo --bar 1:pattern:___script }
compdef __script script
8
  • wow, thanks. I think this works exactly as I wanted. But I have no idea how this works. Could you please explain the logic, and what those many underscores mean? Commented Apr 7, 2023 at 17:30
  • assuming my script name is h is this the whole completion file correct? ctxt.io/2/AACQjj3-FA Commented Apr 7, 2023 at 17:37
  • Yes, it does. The compdef command does the binding of a command (h) and its completion script (__h) Commented Apr 7, 2023 at 18:32
  • With __h, and ___h, you can use any name you want. But you have to be consistent and replace all occurences of __h (or ___h) by what you want (but distinct names). The advantage of this notation is that it makes clear that h, __h, and ___h are related. This makes them different which is required. Commented Apr 7, 2023 at 18:48
  • The syntax 1:pattern:___script defines what should be completed as a first argument. The ___script declare that options should be used. This makes the already declared options available. You may also have a ___h which declares other options which would be added to the list for the first argument only. Commented Apr 7, 2023 at 18:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .