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.

1 Answer 1

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.

1
  • 1
    Man, zsh completion is a doozy... Commented Oct 23, 2015 at 18:43

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.