Scott Hanselman is one of the good people in tech, and his YouTube channel is full of great tech topics explained in his clear, instructive fashion. While watching one of his git videos, I noticed his nice Windows terminal prompts that even indicate git status for him when he’s in a repo directory. You can see his terminal setup in his blog post How to make a pretty prompt in Windows Terminal with Powerline, Nerd Fonts, Cascadia Code, WSL, and oh-my-posh.
Obviously the first thing I did upon seeing this was pause the video and go in search of a way to make my Mac terminal do that crazy prompt thing. As I discovered, it’s easy if you use Zsh, which I already do.
Note: All this below assumes you’re already using Zsh as your shell.
You can see for yourself whether you’re using Bash or Zsh with the echo $SHELL command. You’ll see one or the other of the following results (unless you’ve really gone nuts and instituted yet another shell entirely):
echo $SHELL /bin/bash echo $SHELL /bin/zsh
If not, do a web search for Zsh and homebrew. Current versions of macOS include and default to Zsh for fresh OS installs, but they’re (as usual) many versions behind. Also if you migrated your account settings from an older version of macOS or OS X, you’re probably still using Bash.
Here’s what my terminal looked like before:
It’s not horrible, but it’s kind of plain and it doesn’t even show directories in alternate colors. While my objective when using the terminal is to get work done rather than to look cool, I still liked the git status integration Scott is rocking in his terminal prompts, so clearly there was work to be done.
While there are third-party terminals like iTerm2, I decided that for now I want to keep using the macOS terminal. That requirement meant whatever customizations I made had to work without a new terminal program. So here’s what I did:
Step 1: Install Oh My Zsh
Oh My Zsh is a framework, basically, for configuring the terminal. It lets you install plugins, themes, and make copious modifications to your terminal experience. I installed it using the curl method:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyZsh/ohmyZsh/master/tools/install.sh)"
My existing .Zshrc file was pretty simple, but nevertheless it had some things in it that weren’t in the new .Zshrc put in place by Oh My Zsh. Fortunately, it makes a backup original .Zshrc file called .Zshrc.pre-oh-my-Zsh. Here’s what my .Zshrc looked like before Oh My Zsh:
alias ll="ls -la" alias llrt="ls -latr" alias lltr="ls -latr" alias python=/usr/local/bin/python3 if type brew &>/dev/null; then FPATH=$(brew --prefix)/share/Zsh/site-functions:$FPATH fi python ~/Scripts/random_motd.py alias pip=/usr/local/bin/pip3
So basically things that I needed to preserve and replicate in my new setup were some aliases, including a couple related to Python versions, and a path variable related to Homebrew. I’ve mentioned Homebrew before; I used it to install Hugo on my Mac (among other things).
Although you can dump these things in the new .Zshrc, Oh My Zsh does things a little differently. It creates a directory called .oh-my-Zsh in your home directory. Inside that is a custom directory, and any .Zsh files you create in there will get parsed by an init .Zsh file referenced by the new .Zshrc.
I created aliases.Zsh, brew.Zsh, and motd.Zsh (which calls my message of the day python script). There’s nothing new in those files, it’s the same stuff that used to be in my old .Zshrc, only now it’s in three different files instead of one.
Step 2: Install Powerline Fonts
In order to display the nice arrow-style prompts including path info and git status, you need fonts that can do this for you.
There are a couple different ways to install the fonts. You can download them from github and install them using the following commands from your home directory:
# clone git clone https://github.com/powerline/fonts.git --depth=1 # install cd fonts ./install.sh # clean-up a bit cd .. rm -rf fonts
Alternatively, just go to the Powerline fonts github repo, download the zip file, unzip, and put the fonts in your ~/Library/Fonts directory.
Step 3: Set Terminal Preferences
Even though the new .Zshrc installed by Oh My Zsh declares a theme to use (also installed by Oh My Zsh in the ~/.oh-my-Zsh/themes directory), it seems like whatever theme you choose in terminal preferences under Profiles will override it. Personally, I’m glad, as I have some themes/profiles in Terminal that I like better than what Oh My Zsh comes with.
The most important thing to remember here, though, is to choose one of the Powerline fonts that you installed by opening terminal preference, clicking the Profiles tab, and then clicking the Change… button in the Font section. Otherwise you’ll just see weird symbols in your terminal prompt where the arrow tail and head should go, among other things.
Step 4: Enjoy
Those three things should be all you need to do to get nice prompts that are git aware in your Zsh terminal window. The result will be something like mine in the image below. Note that the git aware prompt is multi-elemental: it has the path prompt on the left showing the path and indicating something about git status via color and some limited symbols, and then on the right hand side of the terminal, there’s a disconnected status line showing exactly what has changed and the status of the local branch compared to the remote branch.
Now that I’ve put the cart before the horse, I’ll go back and do a post about how to move from Bash to Zsh at some point. Unless you have a LOT of custom Bash scripts, it’ll be easy and well worth it, as Apple will quit supporting Bash at some point.