A.8.2. Completion
The zsh completion system is extremely sophisticated.
The main idea is that any time you are about to type something on the
command line, if you hit TAB, zsh will try to
complete it. zsh comes with many defaults for
completion and is also fully customizable.
To get a full set of default completion features, run the following
commands (normally in your ~/.zshrc startup file):
autoload -U compinit
compinit
Now let's look at some examples.
We represent the TAB key in examples as [TAB].
First, zsh is smart about doing completions.
For example, cd[TAB] only
expands directories, thus eliminating completion noise.
g@sifl:pts/7% man zsh[TAB]
zsh zshcompctl zshcontrib zshmodules zshzftpsys
zshall zshcompsys zshexpn zshoptions zshzle
zshbuiltins zshcompwid zshmisc zshparam
Or maybe you want to find out a process name or PID you want to kill:
g@sifl:pts/2% kill [TAB]
9652 pts/2 00:00:00 zsh
9653 pts/2 00:00:00 ps
For finger, it expands users:
g@sifl:pts/7% finger o[TAB]
odin omg oof operator orb
and hosts:
g@sifl:pts/7% finger oof@[TAB]
brak localhost sifl zorak
Using the distributed compdef function, you can
define your own completions, using either your own custom functions
or the completion functions that come with zsh.
For example, the distribution defines the kill
command to use the _pids distribution function to
provide process identifiers. You can also use it to define completion for
other commands, such as the Solaris pstack command:
compdef _pids pstack
Once this is done, you can apply completion to the pstack
command like so:
g@sifl:pts/7% pstack [TAB]
13606 pts/7 00:00:00 zsh
13607 pts/7 00:00:00 ps
Another very useful distribution completion function is
_gnu_generic. This can be applied to any command
that uses the GNU --long-option
command-line option conventions. The zsh
distribution specifies many GNU commands to complete with this function
(such as tar):
g@sifl:pts/7% tar --v[TAB]
--verbose --verify --version --volno-file
And this is just the tip of the proverbial iceberg. There is
much more to the zsh completion system; see
zshcompsys(1) for the (gory) details.