A Simple Guide to Setting Up zsh
Tags: shell, terminal, zsh, bash
Published: 2020-01-04 20:51:59

_This is a cross-post from [my blog](https://goulet.dev/posts/zsh-guide/)._

Recently I decided to switch my default shell to zsh.  I've been using bash for years because... it's been the default.  But macOS Catalina [changes the default shell from bash to zsh](https://support.apple.com/kb/HT208050) and I got tired of the warning message popping up every time I opened Terminal.

![macOS Catalina warning](https://thepracticaldev.s3.amazonaws.com/i/xkxnwblu78xyx1y1xt1w.png)

So, time to switch to zsh and ditch that warning message :smile:.

Now, I'm not looking for an overly-complex setup here.  I'm aiming for a quick and simple setup that results in a fast shell that supports auto-complete and the aliases I'm used to.  One other feature I'd like is showing git branch info when in a git directory.  Let's get to it!

## Steps

### 1. Set zsh as default shell

You can switch the default shell to zsh by running:

```sh
chsh -s /bin/zsh
```

I'm not totally sure "chsh" stands for CHangeSHell but at least that's how I remember it :wink:

> You can see a list of all supported shells by running `cat /etc/shells`

### 2. Install "Oh My Zsh"

The simplest way to get a bunch of great features and keep your setup simple is to install [Oh My Zsh](https://github.com/ohmyzsh/ohmyzsh).  To install just run the following command in your terminal:

```sh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
```

### 3. Set the theme

There are a lot of [built-in themes](https://github.com/ohmyzsh/ohmyzsh/wiki/Themes) you can use.  I personally use "ys" because it shows me git branch info and the input prompt is on a new line (meaning it's at a consistent location regardless of the directory I'm in, so my eyes always know where to look :eyes:).

To use a theme just set ZSH_THEME in `.zshrc`.

```sh
ZSH_THEME="ys"
```

### 4. (Recommended) Add plugins

Oh My Zsh comes with plugin support for things like aliases and command completion.  There are a bunch of [built-in plugins](https://github.com/ohmyzsh/ohmyzsh/wiki/Plugins) that you can add.  Just space-separate the list in the `.zshrc` file:

```sh
plugins=(git npm docker docker-compose)
```

### 5. (Optional) Add your aliases

The git plugin in Oh My Zsh provides a [bunch of git aliases](https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/git/)... however I have some git aliases that are just hard-coded into my fingers so I have to add my own aliases.

> Your custom aliases override the plugin aliases so don't worry about name collisions

To add your own aliases just go to the bottom of the `.zshrc` file and add them.  If you come from bash you can just copy paste what you have setup in your `.bash_aliases`/`.bashrc`/`.bash_profile`.

```sh
# custom aliases
alias repos="cd ~/Source/Repos"
alias gs='git status'
alias ga='git add --all'
alias gf='git fetch --all --prune'
alias gsu='git submodule sync && git submodule update --init --recursive'
alias gc='git commit -m'
```

## If coming from an existing shell...

Some tools add exports to your shell's profile for the tool to work properly.  For me, my existing `.bash_profile` has exports for [nvm](https://github.com/nvm-sh/nvm) and gpg, so I need to copy those over to the bottom of my `.zshrc`.

```sh
# gpg needs this
GPG_TTY=$(tty)
export GPG_TTY

# nvm needs this
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
```

## Reference .zshrc

This is mostly for my own future reference, but you can find [my current `.zshrc` file here](https://gist.github.com/wes-goulet/66553904486fb386fb8de0cadcc700fa).

> Hopefully you found this post helpful, if you have any questions you can find me [on Twitter](https://twitter.com/wes_goulet).