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:

$> chsh -s `which zsh`

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:

$> cta foobar
zsh: correct 'cta' to 'cat' [nyae]?

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:

$> alias -s rb=vim
$> foo.rb

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.

$> hash w=~/Workspace

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.

$> /usr/local
$> pwd
/usr/local
$> ~w && pwd
/home/domnikl/Workspace

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”.

$> ruby
$> ruby foobar.rb
$> ruby test.rb

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:

 $> cd local lib
 $> pwd
 /usr/lib/bin
 

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! :)

Tagged with:
 

6 Responses to zsh – a bash alternative that’s easily customizable with oh-my-zsh

  1. [...] This post is in German, you might want to read my last post about the zsh, which you can find here. [...]

  2. Rick says:

    A note to your readers: (At least on Ubuntu 11.10) your examples don’t work until after you install “oh-my-zsh” (or otherwise make suitable adjustments to your .zsh)

    Further, for those on same system–follow manual instructions for installation of “oh-my-zsh” didn’t work.

  3. [...] other day I blogged about an alternative for bash, zsh. But what if you want to write your own shell that behaves exactly like you want it [...]

  4. Dominik Liebler says:

    Recently, the oh-my-zsh fork from Sorin Ionescu has been renamed to prezto. The new repository can be found here: https://github.com/sorin-ionescu/prezto. I seriously recommend to use this instead of Robby Russells original Oh My Zsh! because of the better implementation of the git plugin. I had several performance issues on various physical and virtual machines with the original version.

  5. [...] might include settings for vim, screen, tmux, bash, zsh or aliases you use for your daily work on these systems. Or some tools you have written to automate [...]

  6. Willynux says:

    Seems nice, I’ll definitely try it

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>