3

I am writing my own completions for a program. I would like to be able to complete quoted words, maintaining the double or single quotes in the completion.

#compdef foo

_foo {
    local strings

    strings=(\
         foo\
         bar\
        'spam eggs')

    _arguments \
        {-s,--string}'[Select a string]:STR:(\""${strings[@]}"\")\
        && return 0
}

_foo

what I'd expect:

foo -s <TAB>
"foo" "bar" "spam eggs"

what it get:

\"foo\" \"bar\" \"spam\ eggs\"

I ended up trying different combinations of nested quotes and escapes almost brainlessly but with no luck, as I was not able to find the relevant docs (really, zsh docs are "dense")

Thank you!

1 Answers1

0

Using compadd with this flag works for me for your example (and yes they are):

-Q

This flag instructs the completion code not to quote any metacharacters in the matches when inserting them into the command line.

https://zsh.sourceforge.io/Doc/Release/Completion-Widgets.html#Completion-Builtin-Commands

#compdef foo

_options() {
  _arguments {-s,--string}'[Select a string]'
}

_params() {
  strings=(\
      '"foo"'\
      '"bar"'\
    '"spam eggs"')
  compadd -Q -a strings
}

_arguments \
  '1:options:_options' \
  '2:params:_params'
Tom Power
  • 1,382
  • 10
  • 10