Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: how can one let completion work *inside* zsh scripts?

Matthias Kopfermann wrote:

> I don't know how to do completion _within_ scripts.
> At the prompt it is not a big problem for me but within scripts I
> have no clue. What messages of the archive should I read
> regarding this question or can you show me how to do that?

First, you use `vared', inside it you have completion.

Then... do you really need a *script*? Or would a function do?

With scripts it's probably better to use compctl so that you don't
have to call compinit, unless you don't care about that extra bit of
init time needed.

With compctl you just set up a `compctl -T' to complete what you want
(filename completion should be standard). If you want completion in
the first column (instead of a tab being inserted) you can use one of
a few tricks mentioned in the `Completion in the first character
position' thread posted on zsh-users in the last few days (including

If you want to use the new completion system (probably preferrable if
the thing can be a function), look at Functions/Misc/nslookup (and
Completion/User/_nslookup) for an example. If you need something else
than filename completion, the `offical' way to do that is to (locally)
define the parameter compcontex and set it to the name of the
completion context that should be used for completion. I.e. to one of
the keys of the $_comps association, i.e. one of the things a `compdef
<func> <context>' has been done for. If there isn't one matching your
needs, add it.

To zsh-workers (and everyone else interested): hm, is this becoming
FAQish enough to make is simplify it? For example we could allow
compcontext to be set to `foo()' to make it just call function
`foo'. And we could allow it to be set to an array and then make
completion offer its elements as possible completions.


Sven Wischnowsky                         wischnow@xxxxxxxxxxxxxxxxxxxxxxx

Messages sorted by: Reverse Date, Date, Thread, Author