A better Shell with oh-my-zsh

On January 8, 2012, in Mac, by Anuj Gakhar

In the last few months, I have found myself using the Terminal on my MacOS more and more. I just find it more satisfying to be in more control of what I am doing and it’s definitely faster too (if you know your commands well). And it was this inclination that led me to find better ways to improve my command-line experience. There are a few things you can do such as setting up aliases for commonly used commands amongst others. There are a whole lot of open source configuration files that people have shared. If you search github repositories for “dotfiles”, there are 8,658 public repositories. That says a lot. There are hundreds of config files, aliases, git config, vim config and what not out there that allow you to personalize your Terminal according to your use case and liking. If you are on a Mac and have not tried some of this stuff yet, you should.

OSX Lion comes with a few different shell’s that you can choose from. The default is bash but if you want you can change that. In your System Preferences, under Users & Groups, right click on the username you want to change the shell for and click on Advanced Options. You should see something like this :-

As you can see, there are a few shells you can use. Mine is /bin/zsh – read more here.  There is also an easy way to change your default shell using the command-line. Here is the command you can use :-

chsh -s `which zsh`

Oh-my-Zsh

oh-my-zsh is a project run by Robby Russell that allows you to configure and personalize your z shell. Quoting from their website :-

A community-driven framework for managing your zsh configuration. Includes 40+ optional plugins (rails, git, OSX, hub, capistrano, brew, ant, macports, etc), over 80 terminal themes to spice up your morning, and an auto-update tool so that makes it easy to keep up with the latest updates from the community.

The installation is pretty easy as well. On your terminal window, you can type this :-

curl -L https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh | sh

or via wget (if you have wget installed) :-

wget --no-check-certificate https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh

All of this is detailed in the README file on the git repository. Once you have installed, you will get a new folder called .oh-my-zsh in your home directory. There are 110 themes and 67 plugins at the time of this post. Which is pretty awesome. The theme I chose is called “gallois” and I also chose a few plugins. My ~/.zshrc file looks like below (only the part that contains theme and plugin selection, I did not include the rest) :-

# Set name of the theme to load.
# Look in ~/.oh-my-zsh/themes/
# Optionally, if you set this to "random", it'll load a random theme each
# time that oh-my-zsh is loaded.
ZSH_THEME="gallois"

# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
plugins=(git rails textmate ruby osx bundler brew github gem node npm rvm rails3 svn)

One thing to note here is that this install script will automatically change your default shell to zsh so you don’t have to manually do it yourself. Once this is done and after I restart the Terminal window, this is what it looks like :-

You can notice that if my current directory is a git repository, it shows the current git branch I am in on the right hand side. It also shows the current Ruby version in use by RVM (if you have RVM (Ruby Version Manager) installed). Apart from this, because of the plugins I enabled, I get a whole lot of aliases auto-configured for me. e.g. here are the git aliases from the git plugin.

# Aliases
alias g='git'
compdef g=git
alias gst='git status'
compdef _git gst=git-status
alias gl='git pull'
compdef _git gl=git-pull
alias gup='git fetch && git rebase'
compdef _git gup=git-fetch
alias gp='git push'
compdef _git gp=git-push
gdv() { git-diff -w "$@" | view - }
compdef _git gdv=git-diff
alias gc='git commit -v'
compdef _git gc=git-commit
alias gca='git commit -v -a'
compdef _git gca=git-commit
alias gco='git checkout'
compdef _git gco=git-checkout
alias gcm='git checkout master'
alias gb='git branch'
compdef _git gb=git-branch
alias gba='git branch -a'
compdef _git gba=git-branch
alias gcount='git shortlog -sn'
compdef gcount=git
alias gcp='git cherry-pick'
compdef _git gcp=git-cherry-pick
alias glg='git log --stat --max-count=5'
compdef _git glg=git-log
alias glgg='git log --graph --max-count=5'
compdef _git glgg=git-log
alias gss='git status -s'
compdef _git gss=git-status
alias ga='git add'
compdef _git ga=git-add
alias gm='git merge'
compdef _git gm=git-merge

This means, I can simply use the shorthand version of these commands. So, git checkout becomes “gco”, git pull becomes “gl” and so on….. you can see how this can save you a lot of time on a busy day…

I have to admit that since I have started using oh-my-zsh, I am pretty much hooked to it. I am sure there is a lot to learn still but I am glad I started.

You can also watch this Episode from RailsCasts on Oh My Zsh which I found useful too.

Happy shell scripting 🙂

Tagged with:  

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 445 other subscribers

© 2011 Anuj Gakhar
%d bloggers like this: