Wednesday 13 June 2012

Z Shell on Mac OS X

I'm a fairly recent newcomer to Mac OS X. I bought my MacBook Pro a little over a year ago, and I've since received an iMac, which I use for most of my development work. Before this, I used a mix of Windows and Linux, but what attracted me to Mac OS X was the combination of a stable and well supported platform with a solid Unix foundation. I spend a lot of time working in the terminal, and having a Unix-like shell is very important to me.

I'm always looking for ways to make my time in the terminal more efficient, and that's why I've recently switched from the default Bash shell to the optional Z Shell. Making the switch is fairly straight forward. Open the System Preferences, and go into Users & Groups. You need to unlock the padlock, and then you can right click on your User (in the left column) and select Advanced Options...

On the Login Shell dropdown list, which should currently say "/bin/bash", select "/bin/zsh". Click OK and close the Preferences dialog box. Now, when you open a terminal window, it's be a Z Shell rather than a Bash shell.

If you have a custom ".bash_profile" file in your home directory, you should open it in your usual text editor and save it as ".zprofile". The syntax is almost identical; the only change I had to make was to my customised PS1 value, which configures how the terminal prompt looks.

Z Shell offers a number of improvements over Bash, such as tab completion, but I want to focus on a project called oh-my-zsh. This is a "community driven framework for managing your zsh configuration." Installing oh-my-zsh is easy, just paste this command into your terminal.

curl -L | sh

Once this is installed, you should close your terminal window.

oh-my-zsh creates a new file in your home directory called .zshrc for configuring the shell theme (colours, mostly) and for specifying which oh-my-zsh plugins to load. Open ~/.zshrc in your usual text editor. Reading the comments, you'll see a few configuration options, such as ZSH_THEME (I use a theme called "jtriley").

At the bottom of the file, you'll see that oh-my-zsh has copied the PATH value from Bash at the time it was installed. If you've created a custom .zprofile file, then you don't need this last line and you should delete or comment it out with a # character.

The really interesting line is the one that looks like this:


At the very least, you should add the plugin "osx", and if you use macports, there's a "macports" plugin too, so your plugins comfiguration would look like this:

plugins=(git osx macports)

Save the file and reopen terminal.

The main point of the plugins is that they provide useful short cuts for us in the terminal. For example, the macports plugin has a shortcut "pup" that runs "sudo port selfupdate" followed by "sudo port upgrade outdated". This means that simply typing the command "pup" updates the macports software and all the ports you have installed.

The osx plugin adds a few interesting integrations between the terminal and Finder. It starts with the command "pfd". Assuming you have a Finder window open somewhere, the command returns the path that is open in that window. While this isn't very useful on it's own, a number of other commands are built on top of it. For example, "cdf" (short for ch finder) will change the terminal to the directory open in the Finder window.

Another variant on this is "pushdf", which is built on the standard unix command "pushd". "pushd" and "popd" are used to maintain a stack of directories, so that you can quickly change between them without having to type the paths into a cd command or use more than one tab. "pushdf" pushes the Finder window's directory onto the "pushd" stack.

Lastly, have you ever been using the terminal and wanted to remove a file or directory but send it to the Trash Can rather than delete it completely? Now you can, without having to go into Finder. Simply enter the command "trash <file_or_directory>" and the offending file will be sent to the trash.

There's a wiki article on the github site listing some of the commands in the plugins, but it's actually really easy to read the source code in the github project and work out what new commands the plugins add. Each alias or function usually creates a new command, and they're all included in Z Shell's tab completion.

Good luck.


  1. This is the clearest explanation I've read about switching to zsh. Thank you!

  2. I just decided to jump into zsh this morning and this article explains it really well. Thank you!

  3. Oops, great explanation, but is dependent on having already set up git on Mac OS.

    1. oh-my-zsh does indeed require git.

    2. Once I got git installed from
      All was well and your instructions worked exactly as expected. Thx and now it's time to pick my fave omz theme and pimp my shell, yey, :-)