1

I have zsh completion for custom script, where one parameter must be URL (ie, string) and then there are several optional parameters: --AAA --BBB --CCC:

_arguments -S : \
    '--AAA' \
    '--BBB' \
    '--CCC' \
    '1: :'

however, the completion only works after URL parameter has been provided, ie:

myscript URL --A<TAB>

this will autocomplete --AAA

But when I have:

myscript --A<TAB>

then it will list the only matching option --AAA, but it will not autocomplete it even if I do --AA<TAB>. I have to type it manually.

When I change my completion script to:

_arguments -S : \
    '--AAA' \
    '--BBB' \
    '--CCC'

autocompletion of parameters works, but as soon as I provide URL, no other options are completed.

So how do I tell zsh completion, that my script expects one URL (ie one string argument - I don't want zsh to start completing filenemes in current location), and optional parameters can be completed either before, or after this URL?

Note: I am not trying to autocomplete URL. I am just trying to have completion work both before and after this one URL string.

1 Answer 1

1

With the code as posted, after myscript --ATab, zsh tries to complete either an option or non-option argument number 1. The action for argument 1 is empty, which falls into what the manual describes under “(single unquoted space)” (this case is actually triggered if the action consists solely of zero or more space characters):

This is useful where an argument is required but it is not possible or desirable to generate matches for it. The message will be displayed but no completions listed.

There is no message to display: the message input is a single space, so the “display” action has no visual effect. However, the internal actions of displaying a message (in _message) are performed, and in particular compstate[insert] is set to be empty. This signals to the user interface of completion that the completion is ambiguous, even if there turns out to be a single valid completion.

The fix is to explicitly arrange for zsh to think that there are no completions, rather than letting it think that there are completions but they can't be generated. Any of the following will do:

_arguments … '1: :()'
_arguments … '1: :{}'
_arguments … '1: : true'

Mind you, if the argument is a URL, you should probably use _urls. This will still result in only completions for options after typing - (as long as no URL starts with a - or you have some very aggressive substring completion settings).

You must log in to answer this question.

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