thewebdev.de

on software development & the web

zsh - a bash alternative that's easily customizable with oh-my-zsh

18 Feb 2012

I think I now what you are thinking right now: "Why do I even NEED an alternative to bash?". And I can tell you I thought the same, too. Bash is perfect, there's auto completion, a command history and all those neat things that help you being productive when using the shell.

And then one day in mid-2011 I discovered the Z shell and once I read about all the nice features it has, I started being even more productive and I fell in love with this tool.

In this tutorial, I will guide you through the installation of zsh, show you some of the many awesome features that this shell brings to your terminal and I'll give you some tips on how to use this shell with your customizations on any machine that you use.

Installation

On Debian and Ubuntu, you only need to install the zsh package via apt, MacPorts and Homebrew both also provide a package zsh. For other systems, please have a look at the official project homepage.

You can start the Z shell with zsh and just play around with it. To make it your default shell (and say goodbye to the bash) run:

[code lang="bash"]
$> chsh -s `which zsh`
[/code]

Features

The zsh is based on the bash, so if you work with it, you may be familiar with many features. The Z shell is still in active development and provides many functions that power users of the bash may miss in it. I'm going to show you some of them here.

Spelling correction

Zsh tries to help you when you send an unknown command and performs spelling correction on your input. Consider this example:

[code lang="bash"]
$> cta foobar
zsh: correct 'cta' to 'cat' [nyae]?
[/code]

Suffix aliases

You may know aliases from bash, but zsh gives aliases even more power: you can assign aliases for prefixes, e.g. if you want to open all files with the suffix .rb in vim do the following:

[code lang="bash"]
$> alias -s rb=vim
$> foo.rb
[/code]

You don't have to type vim foo.rb just foo.rb will then do just the same.

Hashes

On my day-to-day work, I use mostly three or four directories (or subdirectories of them) where I do all programming stuff. One of them is ~/Workspace. To go there, regardless of where I am at the moment, I set up a hash alias that points there. It's simple but saves me a lot of typing.

[code lang="bash"]
$> hash w=~/Workspace
[/code]

It's just another form of an alias and now I only have to do cd ~w to go there. Awesome, isn't it?

auto cd

I think cd is one of the most used commands in any shell and it can be annoying to type that two letters (and the blank) over and over again. zsh allows you to simply leave them and will detect if what you want to cd to is a directory and do the change.

[code lang="bash"]
$> /usr/local
$> pwd
/usr/local
$> ~w && pwd
/home/domnikl/Workspace
[/code]

history

The history in zsh is even more powerful than that in the bash. You may already know CTRL-R to search in the history, zsh has this too. But consider this: you type "ruby" and want to have all commands that started with that term. Just press arrow up and you will only see entries from the history that started with "ruby".

[code lang="bash"]
$> ruby
$> ruby foobar.rb
$> ruby test.rb
[/code]

autocompletion

The auto completion features are one of the main causes why I love zsh. I find it very annoying, that autocompletion in bash is case-sensitive. This is not the case in zsh. And zsh allows programmatic autocompletion, so there's even autocompletion for kill(1) and you can chose the PID with your arrow keys!

globbing

zsh supports advanced globbing, e.g. if you use ls **/*.html, the shell will list you all files in any directory under . that has the suffix html.

advanced cd

cd in the zsh has another nice feature: you can use it to change the directory by substituting parts of the current working directory with a new part. Let's say you are in /usr/local/bin and want to go to /usr/lib/bin, just do this:

[code lang="bash"]
$> cd local lib
$> pwd
/usr/lib/bin
[/code]

These are just the most important features for me. I use them really often in my day-to-day work, if you want get more details have a look at the ZSH guide.

Using zsh from more than one machine

If you own more than one machine, you might want to have all you customizations (aliases and such) on all your machines. I solved this problem with git. Versionize your customizations and put them on github (or another provider). Take a look at my .home repository, it contains all my basic settings.

Feel free to fork and add your own settings :)

using oh-my-zsh

Robby Russell started an interesting project named oh-my-zsh, where zsh lovers share plugins, settings and themes that help you being even more productive on the shell. EDIT: don't use the original as it introduces some bugs and is said to be very slow. Use the fork from sorin-ionescu instead. You can find it here on github. It also allows you easy customization of your shell prompt. Here's mine:

As you can see, when I am in directories that contain a git repository, the shell shows me which branch I'm on (develop). The blue part is the directory I'm in (last part of the path only) and the yellow chars are the hostname. When I am working as root, a bold red label "root" would warn me of that fact.

That's the end of my little overview on zsh, but I hope you liked it. Try the Z shell and you'll see that it's fun and easy. Have fun! :)