I originally switched to zsh from bash back in the day because I wanted to have a prettier shell prompt. Not much has changed since then, but in my quest for an ever more beautiful terminal I’ve definitely learned a lot, and I’d like to think my setup has gotten more streamlined as well:
Follow the steps below to reproduce my terminal setup. I’ve tried these instructions on OSX 10.12 (Sierra), 10.13 (High Sierra), and 10.14 (Mojave) without issues, but feel free to let me know if you run into any.
Prerequisites
Install git if you don’t have it already, either by installing Xcode Command Line tools:
$ xcode-select --install
or by going through the installer prompts that pop up after you run git
for the first time:
$ git --version
Install homebrew:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Optionally, you can install homebrew-cask (I love this because it makes it super easy and fast to set up your computer from scratch!):
$ brew tap caskroom/cask
Installing zsh
Why zsh? For me (I’m not a shell power user by any means, but I do use the command line a lot), I like that there are lots of configuration frameworks out there to make customizing the look and feel easy. I also love the autocomplete functionality. This is a pretty great slideshow with lots of side-by-side comparisons of bash and zsh, if you want to know more.
Install zsh
and zsh-completions
:
$ brew install zsh zsh-completions
Run zsh for the first time:
$ zsh
You’ll get kicked into the zsh first use wizard — select option 0
(“Exit, leaving the existing ~/.zshrc alone.”).
Install & set up Prezto
Why Prezto? I used to use oh-my-zsh as my configuration framework but it was slow af to start up. There are tons of alternative zsh frameworks out there, but for now Prezto is serving my needs just fine.
Delete the .zshrc
file that the previous step created, since we’ll be using Prezto as our configuration framework (I think generating the Prezto config files chokes if .zshrc
already exists, but I’m not 100% sure):
$ rm -f ~/.zshrc
Install Prezto:
$ git clone --recursive https://github.com/sorin-ionescu/prezto.git "${ZDOTDIR:-$HOME}/.zprezto"
Generate your configuration files (copy/paste this as one command):
$ setopt EXTENDED_GLOB
for rcfile in "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/^README.md(.N); do
ln -s "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile:t}"
done
chsh -s $(which zsh)
Change your Prezto theme to powerlevel9k
— this setting lives in ~/.zpreztorc
. Look for a line that looks like this:
$ zstyle ':prezto:module:prompt' theme 'sorin'
and replace sorin
withpowerlevel9k
.
(If you ever get sick of using this theme, Prezto comes packaged with several others by default.)
Install & set up iTerm2 with fonts and colors
Why iTerm2? Much like my preference for zsh, it’s mostly about ease of customization for me. iTerm2 also has some nice features like split panes, and gets updated more regularly than OSX’s terminal. Make sure you’re installing version 3.1.1 or later!
Install iTerm2 (binary at link, or you can do it the cask
way):
$ brew cask install iterm2
Go to Preferences > Profiles > General. Under Command, select “Command” (rather than “Login Shell”) and enter /bin/zsh --login
:
Installing Powerline fonts
For context, Powerline is a status line plugin for vim. Powerlevel9k is one of several themes for zsh that uses Powerline fonts. This doc explains why we need special fonts at all, but the tl;dr is: for cool status icons!
Install Powerline-compatible fonts:
$ git clone https://github.com/powerline/fonts.git --depth=1
$ cd fonts && ./install.sh
$ cd .. && rm -rf fonts
Alternatively, if you’d like more fonts or fonts with more glyphs than are provided in the above repo, you can check out the Nerd Fonts repo, which provides a ton of patched/extended fonts as well as installation instructions. I’d recommend selecting the font you want from the list of available fonts, and installing it the Homebrew way:
$ brew tap caskroom/fonts
$ brew cask install font-NAME-OF-NERD-FONT
If you elect to use Nerd Fonts, you should also edit your .zshrc
file to include the following (documentation here):
POWERLEVEL9K_MODE='nerdfont-complete'
Go to Preferences > Profiles > Text and select one of the optimized fonts that you just installed. I’m using Meslo LG M DC Regular for Powerline, 12 pt, with some tweaks to vertical and horizontal spacing to get things to line up to my liking. You can tweak the spacing by hitting the Change Font button, which opens another preferences window.
Installing color schemes
Install some color schemes:
$ git clone https://github.com/mbadolato/iTerm2-Color-Schemes.git
In iTerm2, go to Preferences > Profiles > Colors. In the Color Presets dropdown, select Import… and navigate to the directory where you cloned iTerm-Color-Schemes
just now. Once there, you’ll want to navigate inside the repository to /schemes
and select the files inside (the list should look like this), then hit Open.
Now, you should be able to select the color preset you want to use from the Color Presets dropdown (in the screenshot up top, I’m using Fishtank). If you need help deciding, the iterm2-Color-Schemes
documentation has screenshots of each theme for you to preview!
Feel free to delete your cloned version of the iTerm2-Color-Schemes
repo.
Bonus!
Customizing your prompt
You can edit and update what specifically appears in your left and right-hand prompts. The Powerlevel9k docs explain the options and what the defaults are. Per this zsh doc, I chose to put my customization in .zshrc
. I still sometimes tweak my settings, but this is my current prompt config:
export DEFAULT_USER=$USER
# not currently being used, but necessary to display the right user context in my promptPOWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir vcs)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status root_indicator background_jobs virtualenv time)
More exciting separators 🔥
You can download even more glyphs (this time to use as Powerline separators) from Powerline Extra Symbols.
And that’s it!
Known issues
For some reason, my setup doesn’t have the right glyph to display the icon for when there are untracked files in git. I FIND THIS INFURIATING, but haven’t dug into fixing it yet. Here’s a related and inconclusive Github issue on the topic.
Edit (3/27/2018): Thanks to this response from Dominic Ritter I was able to do some more digging and figured some things out:
- The
?
is a feature, not a bug: Even the Nerd Fonts patched version of Meslo shows a?
as the “untracked files” indicator, but other fonts have different icons to represent this state (often based around the idea of a question mark, but more icon-y in appearance). I’ve updated the instructions above to include steps to getting different/fancier fonts. - For more than you ever wanted to know about Meslo in particular, see here and here. Every time I think I understand something about fonts, I realize… it’s really just the tip of the iceberg 😂😭
Useful links
I tried to include the links to relevant/useful documentation and Github issues inline, but here are some additional general resources!
These are the tutorials I used as a starting point for setting up my terminal the first time:
- http://www.codeblocq.com/2016/09/Pimp-up-iTerm-with-Zsh-and-prezto-on-Mac-OS-X
- https://github.com/bhilburn/powerlevel9k/wiki/Install-Instructions
If you run into issues with Powerlevel9k, the wiki is super useful.
If you’re interested in digging more into the ✨magic of zsh✨, this tutorial is a great starting point.
And finally, shoutout to Jen Hamilton, who has an awesome terminal customization walkthrough as part of her series on setting up an iOS dev environment!