Switching bash with zsh

I’ve been running OS X since the beginning back in 2001. During that time, the system have gone through a lot of changes, and while some has improved a lot of visual effects, and have a great impact on the way we perceive our tasks at hand, some are more invisible, but also have a large effect on the way we work.

I usually spend 20-30% of my day in Terminal, doing administrative tasks, ssh’ing to the servers I use for development and for my work, watching logs, debugging etc. This might not be typical behaviour for an OS X user (Which is good, as I think Terminal should be an addition for those who wants the power of it, and not a necessity for “regular” users) , but I find working directly with Terminal to improve my workflow when it comes to the previously mentioned tasks.

As some Mac-users will know, OS X currently comes with bash as the standard shell. Even though bash is often perceived as one of the best shells available in the Unix-community, quite a lot of other shells exist, all with different features, and approaches to shell-work. A couple of years ago I decided to try out Z Shell, also known as zsh, and I’ve never regretted this.

Why run zsh ?

To begin with, it’s not even a big deal switching or trying zsh out. If you are currently using bash, you’ll feel right at home. Zsh uses a lot of the same shortcuts, has support for most of the features bash uses, and this means the only thing you’ll actually notice, is how the extra features just make things easier. Some of the key features for zsh are:

  • Nice autocompletion for commands, aliases etc. Zsh allows you to tab through completions if there are multiple possibilities, showing possible completions in a nice grid, browsable even by arrow keys, to make it simple and fast to find exactly the completion you want.
  • Autocompletion for remote files. If you are using scp to transfer a file to a remote server, zsh will figure out what you are doing, so if you try to autocomplete while typing the remote part, it will try to connect via ssh and actually perform autocompletion on the remote side.
  • Functions in addition to aliases. It’s possible to create entire routines as zsh functions with arguments, allowing for complex functionality for running multiple commands in succession easily.
  • Share history across sessions. When running multiple instances of zsh at the same time, the instances are able to share a common history, rather than being limited to only each its own. One of the most frustrating things to a bash user is typing in a long command, then searching for it again the next day via Ctrl+r, only to find it nowhere in the history.

Ok, so how do I change my shell to zsh ?

Enabling zsh in OS X Leopard is really easy. Previously, you had to install zsh from MacPorts, but in Leopard it’s part of the base install, and as such is easily accessed. try writing “zsh” in your bash, and bang! You’re running zsh! To set zsh as your default shell in OS X, and make it run every time you start Terminal, load up Terminal’s settings, and set your default theme to run the command “zsh” on startup.

To change it in older OS X versions, you should be able to use the chsh command. But there are other suggestions as well.

But, but, it’s not working like you promised ?

You’ll need a .zshrc file to take full advantage of zsh. The .zshrc is zsh’s configuration file, and will setup most of the features I’ve mentioned earlier. There’s a couple of ways of getting hold of a .zshrc. You could google for it, and you’ll find a bunch of examples. Another way is to take a look at my sample file . This is not an exact copy of my running .zshrc, but it has a bunch of the most common setups.

To make it work, you have to save the file as .zshrc in your home directory.

Be aware that zsh holds a bunch of more features than I’ve talked about here, and there’s much to explore. Feel free to use my sample file as a starting point, and do change it anyway you’d like. If you find any other useful features in zsh, please let me know, and I’ll make some notes about it here.

Further reading

If you want to know more about the glorious world of zsh, try checking out the official zsh website, containing documentation, the manual as well as some pointers to getting started with scripting in zsh.

Zsh homepage

My sample .zshrc

Zsh Wiki

Tags: ,

5 Responses to “Switching bash with zsh”

  1. Claus Witt
    March 5th, 2009 at 3:26 pm

    For some reason the line “EDITOR=emacs” slipped into your .zshrc file. :-D

    A nice read I must say. Especially because I have heard you talk about the shell many a time, but I haven’t actually got around to trying/using it – I might just do that now.

  2. Jesper
    March 5th, 2009 at 3:30 pm

    Oh really .. that must be a mistake, no propaganda there (Read: Emacs rocks!) ;)

  3. rakesh
    December 8th, 2009 at 4:59 am

    I used your .zshrc and it didn’t work . Got these messages.

    can’t find termcap info for screen [9]
    bad option: -U [39]

  4. Pascal
    December 30th, 2009 at 1:03 pm

    “Functions in addition to aliases”
    this still works in BASH.

    but zsh is a good choice, anyway.

  5. Sindhu S
    April 28th, 2010 at 6:07 am

    I was wondering what is the equivalent for –rcfile option of bash in zsh? I.e., I want to load a different zshrc file of my choice…

Leave a Reply