Don't understand german? Read or subscribe to my english-only feed.

mika’s advent calendar – day 19: zsh completion

One of the reasons why Zsh rocks so much is its completion system. Tab, tab, tab….

Using default Zsh without an useful configuration? Then start with loading the completion system:

autoload -U compinit && compinit

Now check out the completion system with tools that provide lots of options, like for example gpg and rsync. Then continue playing with tab completion using ssh/scp (make sure to use ssh-agent or ssh with option ControlMaster to avoid typing your password again and again…):

scp <tab>
scp user@<tab>
scp user@host:<tab>
scp user@host:/path .

BTW: running ‘ls /u/lo/b<tab>’ should list the content of /usr/local/bin.

Customising the completion system is possible using zstyle. For example to use colors inside completions use the following configuration snippet (assuming $LS_COLORS is set on your system, if not try running ‘eval $(dircolors -b)’):

zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}

If you want to write your own completion mechanism for a program start by exploring the existing completion functions (on Debian systems usually being available at /usr/share/zsh/functions/Completion/). You might consider writing a completion for a program which isn’t supported by Zsh’s completion system yet. You can get a list of those tools running:

check_comp() {
((! $+_comps[$1])) && print "$1 has no completion function yet"
}
setopt nonomatch
for f ($^path/*(:t)) check_comp "$f"

Find all the glory details about Zsh completion in the Completion System documentation, chapter 6 of the Zsh guide and the manpages zshcompctl, zshcompsys and zshcompwid. Notice that zshcompctl is the old style of zsh programmable completion, zshcompsys is the new completion system and zshcompwid are the zsh completion widgets. Several configuration examples are available in grml’s zsh configuration and the COMPLETION section of the zsh-lovers manpage.

Oh, and if you understand german I strongly recommend getting a copy of the book “Zsh – Die magische Shell”, it provide a nice introduction into the completion system.

One Response to “mika’s advent calendar – day 19: zsh completion”

  1. peter Says:

    you make great posts. thanks for the calendar!