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

Re: trying to learn zstyle

As a preface:  the stuff discussed below is new since 3.1.6.  I should
perhaps explain to zsh-users readers not on zsh-workers that the latest
development version of the shell is now available on the archive and
mirrors in the `development' subdirectory.

Shao Zhang wrote:
> Hi,
> 	I am trying to learn zstyle to do command completion, but I am
> 	very very confused how everything works after reading the doc
> 	and trying out.

It's quite complicated.  I'm working on the chapter of my zsh guide which
deals with completion to try to help.

> 	I have the following lines in my .zshrc:
> 	zstyle ':completion:*:hello:*:hosts' hosts $HOSTS
> 	zstyle ':completion:*:mtvp:*:hosts' hosts $HOSTS
> 	zstyle ':completion:*:telnet:*:hosts' hosts $HOSTS
> 	But all of them behaves differently.
> 	hello [TAB] gives me a completion of list of files in my current
> 	dir.
> 	mtvp [TAB] give me a completion of -display    -geometry

(It's not part of your question, but if you want to use the same list of
hosts in all contexts, you can set
  zstyle '*' hosts $HOSTS
which simplifies things a bit.)

The style `hosts' just gives values for cases where hosts are completed ---
it doesn't say where hosts are to be completed.  You can think of styles
just as a sort of shell parameter, but (as you realised) with the ability
to have different values in different contexts.  You need to tell the
system that the command `hello' has to take hosts as arguments.  The
easiest way to do it is this (after loading completion, of course):

  compdef _hosts hello mtvp

> 	telnet behaves the way I want, which gives me a list of hosts
> 	defined in $HOSTS.

That's because it's already set up to use hosts.  You can check this by
  print ${_comps[telnet]}
which gives `_telnet' (surprised?).  If you look in the function _telnet,
the hosts handling is buried inside the `combination '' hosts-ports-users'

We ought to add things like the handling function to the output of ^Xh
(that's what tells you the context you're completing in).  It helps in this
case a bit, because if you did it with `hello' you'd have got:

tags in context `:complete::hello:': 

which would have told you that the only valid tags were files and
all-files, but not hosts.

Peter Stephenson <pws@xxxxxxxxxxxxxxxxxxxxxxxx>

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