Quick Intro to zsh: auto complete

By | 2011/08/19

zsh or the z shell is an interactive shell that builds upon features of other shells such as bash, ksh, and tcsh. zsh is great for advanced script monkeys but also is an excellent simple bash replacement for the regular joe user.

Here is a quick intro to the auto complete features of zsh!


On Debian / Ubuntu, install zsh:

$ sudo apt-get install zsh

Then to get a zsh shell, run:

$ zsh

On OS X, zsh is installed by default. Just run zsh to get the shell.


Configuring zsh

zsh shell options can be set in:

~/.zshrc

Below is a basic config to get you started:

# Set up the prompt

autoload -Uz promptinit
promptinit
prompt adam1

setopt histignorealldups sharehistory

# Use emacs keybindings even if our EDITOR is set to vi
bindkey -e

# Keep 1000 lines of history within the shell and save it to ~/.zsh_history:
HISTSIZE=1000
SAVEHIST=1000
HISTFILE=~/.zsh_history

# Use modern completion system
autoload -Uz compinit
compinit

zstyle ':completion:*' auto-description 'specify: %d'
zstyle ':completion:*' completer _expand _complete _correct _approximate
zstyle ':completion:*' format 'Completing %d'
zstyle ':completion:*' group-name ''
zstyle ':completion:*' menu select=2 eval "$(dircolors -b)"
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' list-colors ''
zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=* l:|=*'
zstyle ':completion:*' menu select=long
zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
zstyle ':completion:*' use-compctl false
zstyle ':completion:*' verbose true

zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'
zstyle ':completion:*:kill:*' command 'ps -u $USER -o pid,%cpu,tty,cputime,cmd'

Autocomplete

To see some zsh autocomplete craziness, try this:

Type cd followed TAB with no space

cd[TAB]

You can then tab around to any different commands beginning with the letters ‘cd’ or also shell parameters that begin with ‘cd’.

01_zsh

And somewhat different, try cd space TAB:

cd [TAB]

This lets you then TAB around for available directories.

02_zsh 03_zsh

Even more wild, typing this:

/u/l/b[TAB]

…autocompletes to /usr/local/bin/

04_zsh 05_zsh

Autocomplete command line options

You can also use TAB autocompletion for command line options to many programs.

I use imagemagick quite a bit. It has a ton of options. With zsh, I can type:

mogrify --[TAB]

Then zsh autocompletes all available options to scroll through and select.

06_zsh

This is only a tease of the power of zsh!

Below are some additional links to check out:

Start here http://zsh.sourceforge.net/Intro/intro_toc.html

Mega guide http://zsh.sourceforge.net/Guide/zshguide.html

RTFM http://zsh.sourceforge.net/Doc/Release/zsh_toc.html

wiki http://zshwiki.org

sourceforge page http://zsh.sourceforge.net

Cool!