/ Productivity

Setting up ZSH on your mac

A few years back I started using a Mac at home for general computing and
editing my photographs. I didn't look back at my Windows computer. Windows 8 is sort of meh
and the Mac hardware just works as opposed to my last PC that failed at least once every
year.

Now that I've used my Mac for a few years I am discovering even more about it that's great.
One of the things I've found that is also great about the Mac is that it has a unix shell.
Unix shell implementations are the best around, no matter what you use on there. Windows took
a very long time to catch up with Powershell. Even now I think bash and zsh are still the best
around. Maybe not so much for their scripting languages which have a weird syntax, but more
so because there's so much knowledge on the internet on how to use them.

The most recent discovery is the zsh shell. The Z-shell as it is officially called is a shell
that looks like bash, but has some features that I personally like better than the same functionality
in bash. It's personal, but hey it is my weblog, so why not try to convince you to use it too on your Mac?

What makes ZSH so great

ZSH has some really cool features, especially for the console junkies amongst us. Here's a few
that I found very useful to work with that don't come with bash:

  1. The tab auto-complete is interactive. Enter a partial name of a directory and press tab
    to autocomplete it. Next, tab through all the items and press enter when the item you want is
    selected in the list. This saves keystrokes, in bash you need to manually type in the name once
    you've found it. Or be lucky enough that there's just one item that matches.

  2. Path autocompletion. When enter /u/l/b and press tab, zsh will autocomplete this to
    /usr/local/bin automatically. Pretty neat, if you want to be really fast around the filesystem.

  3. Git command completion. Working with Git from the commandline? Enter git in the zsh shell
    and press tab to autocomplete it. Bash doesn't that kind of stuff for you mister!

  4. Extended aliasing. Not much used I think, but it is nice to have. Set an alias like this:
    alias -s html=chrome and when I enter index.html on the terminal it will start Chrome with
    the index.html file.

Installing ZSH on your Mac

Well not so much installing, because it is already installed on your Mac. To use zsh simply
start your terminal and type the following command:

sudo chshl -s zsh

This will set the default shell to be zsh. Restart the terminal program to use the new shell.

Oh my zsh, there's more!

Once zsh is installed, you can upgrade the experience even more with oh-my-zsh. This is a program
available on github that adds more cool stuff like themes and additional commands to your zsh shell.

zsh is really powerful, but the setup can be hard to do by hand. This is why Robby Russel created
oh-my-zsh. This tools configures your shell to be a little more useful than the default.

To install the extension to the zsh shell go to the Oh-my-zsh github page
and follow the instructions there.

Once installed you will be given a .oh-my-zsh folder in your home directory which contains everything
from this tool. Oh-my-zsh also customizes your .zshrc file in the home directory to contain the necessary
config data to enable this tool.

The power of zsh is in the custom themes, which allow you to display git information in the prompt of
your shell, such as the branch and the status of that branch. It also contains a huge number (150+) plugins.
Ranging from developer specific extensions to more general purpose things. Check out the the
~/.oh-my-zsh/plugins folder to find out which plugins there are.

Tips and tricks

Zsh on its own is great, it gets better with the customizations that oh-my-zsh offers, but there are
a few things that I've added myself to complete the setup for my computer.

One of the things that really bothered me is that Alt+Left and Alt+Right doesn't work in zsh.
Normally you use these commands to move back and forth between words in the current command.
But instead of doing that, the zsh shell prints ackward characters on the terminal.

To fix this, type in cat .
Then type in Alt+Left and then Alt+Right.

Remember these keystrokes for now. And add the following lines to your .zshrc file:

bindkey "<alt+left>" backword-word
bindkey "<alt+right>" forward-word

Replace the keystrokes in this snippet with the actual keycodes that you've discovered with
the cat command you entered earlier.

Apart from this hack I use a number of plugins coming from oh-my-zsh

  • git - Obviously, I use git a lot and this saves a lot of time entering all those commands
  • git-flow - A companion to the above plugin.
    This makes it easier to work with the git flow, a popular method of working with git repos
  • osx - Extras for OS X.
    Use this on your mac to enable commands like trash to move stuff to the trashcan instead of deleting it.
  • npm - NPM command completion
  • node - Node command completion

As for themes, there's a 100 to choose from, some more useful than others. But that's what you get with 100 themes for one shell.
I personally settled for the mh theme, but I might change it to something a little more pleasant to the eyes later on.

Conclusion

Discovering ZSH and the oh-my-zsh stuff is one very big happy hackers delight.
If you have any tips, themes I should try or plugins that I should be using, let me know!

Willem Meints

Willem Meints

I write and talk about new technology. I like to help others get the most out of the technology they use. Within Info Support I work together with others to help projects start AI and other tech.

Read More