3

I'd like to set up zsh completion for a command which has a single argument of the form scheme:parameter.

Suppose I have commands which can return all possible schemes, and all possible parameters. What's the completion function which completes from the list of schemes until the colon is typed, and then completes from the list of parameters?

Completing from a single list is simple, something like:

function _prmt() { _alternative 'args:args:($(prmt --completions))' }

but I don't know how to get it to recognise a separator.

3

One way is to look for the : and behave different like depending:

#compdef foo
# put this file into a $fpath directory (typically one you create
# and prefix to that array) then `rm ~/.zcompdump && exec zsh`
# until work-making (a temporary `print -z "foo "` in your .zshrc
# may also speed debugging, as then you can go directly to the
# mashing-of-tab stage)

local curcontext="$curcontext" state line
local suf
typeset -A opt_args

_arguments -C -s \
  '1: :->dofoo' \
  && return 0

case "$state" in
  dofoo)
    if compset -P '*[:]'; then
      _values "parameters" $(_call_program getparam ls /)
    else
      if compset -S '[:]*'; then
        suf=()
      else
        suf=( -qS ':' )
      fi
      _wanted "schemes" expl "scheme" compadd $suf[@] $(_call_program getscheme ls /)
    fi
  ;;
esac

The ls / bits will need to be your appropriate commands, perhaps with additional complications to properly split the output into a list, depending on what those programs emit, etc.

How I learned this: mostly cd $fpath[-1] and then poking around in especially the _chown and _users completions, which have completion for : handing. More info.

  • Man, zsh completion is a doozy... – jogloran Oct 23 '15 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.