← back

2017-05-13: getting started with zsh


Quite a few years have passed since I've used csh or the korn shell. The modern Linux distro seems to favour the bash shell, and it has become as ubiquitous as the operating system itself. Perhaps for good reason, it was the original GNU alternative to the bourne shell.

As any righteous disciple of the technology will tell you, productivity is key to any working environment. Plus crufty code that leads to flaws or problems is an everpresent concern. Are there maybe alternatives worth considering?

One in particular, fish, seemed interesting, but after a short discussion with some collegues, they mentioned zsh. Admittedly, I wasn't immediately convinced to try it, however with several Google searches it appeared that a very large number of users swear by it. So why not give it a spin?

Since we need to overcome the inertia of bash, a bit of excitement is needed. Gander at the flashy and illustrious prompt themes.

prompt -p

Feels good? Bland shells tend to be a bit of bore and I do like the elite2 theme, but each to his own. The prompt choice can be made permanent by adding the following to the .zshrc file:

#
# Prompt settings
#
autoload -Uz promptinit
promptinit
prompt elite2 blue

Like bash, the zsh allows for a customization of the individual components. Doing so is easy, simply add a zstyle after autoloading any given part of zsh. In a similar vien, for the command completion, I used the below settings:

#
# Command completion settings
#
autoload -U compinit
compinit
zstyle ':completion:*' menu select
zstyle ':completion:*:descriptions' format '%U%B%d%b%u'
zstyle ':completion:*:warnings' format '%BSorry, no matches for: %d%b'
setopt COMPLETE_ALIASES

Now bash can recall commands by means of Ctrl-r, but Zsh can also quick recall recently typed commands upon typing a letter or two and pressing the up/down arrow. Setting it up is harder but definitely recommended, since it can save you potentially a couple Ctrl-r keystrokes during the ol' daily grind.

#
# Command history options
#
export HISTSIZE=2000
export HISTFILE="$HOME/.history"
export SAVEHIST=$HISTSIZE
setopt hist_ignore_all_dups

#
# Quick command recall
#
autoload -Uz up-line-or-beginning-search down-line-or-beginning-search
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search

[[ -n "$key[Up]" ]] && bindkey -- "$key[Up]" up-line-or-beginning-search
[[ -n "$key[Down]" ]] && bindkey -- "$key[Down]" down-line-or-beginning-search

If you recently typed some long and painfully easy to forget command, typing the first few letters of it and pressing the up arrow will immediately recall it. Neat, eh?

Yet the real beauty of zsh is the autocompletion. The bash variant is rather tab-tastic; to get the path of a greatly nested directory quite a few tab presses are required. With zsh you need only fill out the first few letters like so:

cd /u/s/v/a/

After a single tab press, zsh will convert it to the following:

cd /usr/share/vim/addons/

Presto! Your directory path is completely filled out. Quite the timesaver on very long directory paths. For command completion, simply start entering a command like so:

sndfile-

The completion settings mentioned earlier set it to the 'menu' selection mode. So after a simple tab stroke the user is presented with a list akin to the below.

sndfile-
external command
sndfile-cmp     sndfile-deinterleave sndfile-metadata-get sndfile-regtest
sndfile-concat  sndfile-info         sndfile-metadata-set sndfile-resample
sndfile-convert sndfile-interleave   sndfile-play         sndfile-salvage

Tab again, and then use the arrow keys to select the desired choice. Simply enough, and elegant for dealing with many different commands at once.

Zsh supports the recursive nested file handling natively, so if you have a sudden need to view all of the header files in a given location, you need only type:

ls /usr/include/**/*.h

Afterwards you will be presented with a list of every header present in every folder and subfolder and subfolder and so on.

Also worth noting is that zsh contains a number of useful plugins. One of the fancier official plugins is the syntax highlighting. Go ahead an enable it in your zshrc:

source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

Try and type a command. Wrong command spellings are highlighted red whilst correct command spellings are green. Certainly helps with those tricky spellings and makes the console a bit more Christmas. What's not to love?

That covers my take on zsh, and arguably, it does have much to offer the average Linux developer or administrator. Doubly so considering much of this functionality is present without the usual bash hacks or elongated .bashrc files.

Maybe at some later date I might consider a further look at zsh functionality, or potentially give the fish shell a chance if I get enough people asking about it.