Dev Snaps

Snapshots of Daily Development

Setting up the Terminal for Development in OS X

Chances are, if you are developer, you have used the terminal before. If not, you should. From version control tools like git to ssh, there are numerous reasons to use the terminal to make your life easier. Also becoming familiar with the command line will only help you later on when you graduate to managing your own Linux server.

OS X comes with a default terminal client which uses bash and honestly isn’t all that user friendly. I have ditched it in favor of iTerm (a terminal client) and Z Shell (ZSH – a command line interpreter). ZSH has some powerful features like prompted typo correction, autocompletion, and prompt customization. ZSH also boasts a long list of plugins which can help you manage your git repositories, highlight syntax errors, and launch text editors from the command line. Plus, lets be honest, it looks really cool.

Step 1: Setup iTerm

The first step to setting everything up is downloading the iTerm client. It can be found here: iTerm

After you have unzipped the file, move the application into your applications folder and start it up. When you open it, it will seem very familiar to the default terminal program. If you open the application preferences, you will see a variety of customization options. You can even import custom color schemes.

Under Profiles > Terminal, make sure the Report Terminal Type is set to xterm-256color as shown in the image below.

Step 2: Install ZSH

To make managing your ZSH configuration easier, I recommend using oh-my-zsh, an open-source framework.

Back on the command line in iTerm make sure you are in your home directory (Users/yourUserName) and type:

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

The installer should run automatically. Restart iTerm to launch a zsh terminal.

Step 3: Editing the Config File

Now that we have ZSH up and running, its time to enable some features. Run the following command:

1
nano ~/.zshrc

This will open the ZSH config file in nano for you to edit. The first thing you will want to do is pick a theme. Available themes can be found here. I like the agnoster theme(seen above), but it is one of the more difficult themes to get working properly because you will need to install a powerline patched font and a custom color scheme. Lets go through it step by step. Change the theme by changing the following line:

1
ZSH_THEME="robbyrussell"

Like so:

1
ZSH_THEME="agnoster"

You can hide your user information in the terminal by adding this next bit on another line:

1
DEFAULT_USER="yourUserNameGoesHere"

As you scroll down in the configuration file, you will see a portion that looks something like this

1
2
3
4
# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/p$
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
plugins=(git)

The line that starts with plugins is the line we are going to edit to include some default plugins that come bundled with oh-my-zsh. Change it to include the sublime plugin and the ruby plugin. (You can change these to meet your needs)

1
2
3
4
# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/p$
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
plugins=(git ruby sublime)

The sublime plugin will allow you to launch Sublime Text 2 or 3 from the command line to edit text files(instead of nano). If you don’t have Sublime Text 2 installed, you can find it here: Sublime Text 2 Type CTRL-X to exit nano, and choose Y when prompted to save. You will also be prompted for the filename to save, but just hit ENTER to overwrite the current config file. Now that we have saved the config file, we need to reload it to affect the settings in ZSH. Type:

1
source ~/.zshrc

ZSH will load the config file and you should see the theme load. It may look different than the image you saw in the theme preview. We still need to setup our color scheme and font.

Step 4: Adding a Custom Color Scheme

To change the color scheme, in iTerm go to Preferences > Profiles > Colors. Click on the Load Presets button to load a preset color scheme. If you have the Solarized Dark color scheme already, simply select it.

If not you can find it here: Solarized Color Scheme. Once you download and extract the files, back in the Load Presets option in iTerm, click Import. Navigate to the directory where the extracted files are and find the iterm2-colors-solarized folder. Inside this folder you can select the themes and click Open to load them. Now select Solarized Dark as the color scheme.

Step 5: Importing Custom Fonts

Now that we have the colors setup we need to change our font. If you dont have a powerline patched font installed you can find the fonts here: Powerline Fonts. The easiest way to install the font is to download the zip file. Once you have extracted the contents, in iTerm go to Preferences > Profiles > Text. You will see fonts for Regular and Non-ASCII font. Click on Change Font. The fonts window will open up. In the fonts window, you will see a cogwheel icon for settings. Click on this and then choose Manage Fonts.

The fontbook window will open and you will see a screen similar to the image above. Click on the plus symbol towards the bottom middle of the window (not the one on the far left) to add fonts. A browse window should open and you simply need to navigate to the directory where you extracted the powerline fonts. You can select all of the subfolders in the window and click Open to add them all at once.

After the fonts have been added, close the fontbook window. Back in the fonts window for iTerm, you can now select powerline fonts. Hint: You can use the search field and type powerline to view only the powerline enabled fonts. Select Source Code Pro for Powerline for both font options. I use 18pt, but you can use whatever size font you would like to.

Review

The zsh terminal should now render correctly and you should see something similar to the image below. If you dont see the yellow or green portion, it is because you are not in a directory that is part of a git repository. Navigate to a git repository and you will see either a yellow or green arrow next to the directory listing. Yellow is for repositories that have uncommited changes, and green is for a repository that has no uncommited changes.

You should now be familiar with what it takes to customize oh-my-zsh now. You can edit the config file again to make changes or to add additional plugins. You can also add aliases (shortcuts) in the config file. To read more about oh-my-zsh, checkout the github page. Be sure to lookout for my next post, Adding a Custom Plugin to oh-my-zsh, sometime next week. If you run into trouble, reach out in the comments section or email me.