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

Re: Why doesn't cd ignore files when you type say "cd fred*"



zzapper wrote:
> >
> >bindkey '\e*' match-word
> >zstyle ':completion:match-word::::' completer _all_matches _match _ignored
> >zstyle ':completion:match-word:*' match-original both
> >zle -C match-word complete-word _generic
> 
> Oliver that works great but what is all that gobbledegook?

It's not easy to explain that fully in a short e-mail.

Hopefully you're already familar with bindkey. It allows you to bind a
key or key combination to an editor command. This binds escape * to
`match-word'.

In addition to lots of builtin editor commands, zsh allows you to
define your own. The `zle -C' command here creates the match-word
editor command. Editor commands which do completion are a little
different so they are defined with `zle -C' instead of `zle -N'.

To implement a user-defined editor widget, a function has to be
written. In this case, we use _generic which is an already existing
completion function. _generic hooks in to the existing function based
completion system. With it, escape * is now going to behave like a
second tab key and do completion. With the zstyle commands we make it
behave differently while still doing completion.

zstyle is a powerful command which is used for configuring many aspects
of completion. It is similar to setting shell options or variables but
a particular "style" can have a different value in different contexts.

You may have already used zstyle in your startup file. The `completer'
style is a very common one. For example, you may use something like:

zstyle ':completion:::::' completer _expand _complete _approximate

This defines the default completer so will be used for your tab key.
This would allow completion to do expansion (expanding file patterns
like fred*), then normal completion and then approximate completion
(which allows for errors in what you have typed).

Above, I used ':completion:match-word::::' as the context. This
restricts the style to applying for our new match-word editor command.
So escape * now does completion but uses a different set of completers.
The important completer in this case was _match. It alone would do the
job you were asking about. The other two completers (_all_matches and
_ignored) just add some extra functionality.

The same goes for the second (match-original) style. That just allows
you to go back to the original string you had typed (such as fred*)
more easily if completion finds several matches.

I hope that makes it clearer. Completion is very configurable and this
isn't the simplist example to start with.

Oliver



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