The Shell on my Mac

On my previous Mac I used powerlevel9k to both give my shell a nice look, and to add some extra metadata to my prompt when working with git repositories (which for me is most of the time). I recently got a new Mac and somewhere between following the install instructions and importing my old .zshrc, I ended up with a corrupted looking shell prompt, which I could not seem to fix.

So, I decided to reinstall all the shell stuff I use from scratch, along the way I discovered powerlevel10k which claims to be much faster than powerlevel9k.

This blog article is really for my own reference on how to setup a nice shell on a Mac, should I ever encounter such problems again. It might also be useful for anyone else who wants a fancy shell on their Mac.

The installation consists of iTerm2, Zsh, Oh My Zsh!, and powerlevel10k, and it assumes that you have Homebrew already installed.

Install iTerm2

From the Mac Terminal.app:

$ brew cask install iterm2

Then close Terminal.app and launch a new iTerm2 terminal and follow the instructions below in iTerm2.

Install Z shell

$ brew install zsh

Z Shell Configuration

My basic ~/.zshrc reflects the fact that I have several tools for Java, Rust and Node installed, and that I have a little script that generates a nice custom MOTD (Message of the Day) for me; anyone else can likely ignore this configuration.

fpath=(/usr/local/share/zsh-completions $fpath)

export PATH="/usr/local/bin:$PATH"
export PATH="$HOME/.cargo/bin:$PATH"
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
export PATH="/usr/local/maven/bin:$PATH"

export JAVA_HOME="/Library/Java/JavaVirtualMachines/zulu8"

export EDITOR=vim

export NVM_DIR="$HOME/.nvm"
source /usr/local/opt/nvm/nvm.sh

# My custom MOTD
$HOME/random-cowsay-fortune.sh

Install Oh My Zsh!

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

Updated Z Shell configuration

Installing Oh My Zsh will make changes to your Z Shell configuration file ~/.zshrc, you can then further modify or customise it, mine looks like:

export ZSH="$HOME/.oh-my-zsh"

ZSH_THEME="robbyrussell"
CASE_SENSITIVE="true"
HYPHEN_INSENSITIVE="false"
HIST_STAMPS="dd/mm/yyyy"

plugins=(
        brew
        colored-man-pages
        docker
        git
        mosh
        mvn
        osx
        ripgrep
        rust
        sbt
        scala
        sublime
        vscode
        xcode
)

source $ZSH/oh-my-zsh.sh


# User configuration

export PATH="/usr/local/bin:$PATH"
export PATH="$HOME/.cargo/bin:$PATH"
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
export PATH="/usr/local/maven/bin:$PATH"

export JAVA_HOME="/Library/Java/JavaVirtualMachines/zulu8"

export EDITOR=vim

export NVM_DIR="$HOME/.nvm"
source /usr/local/opt/nvm/nvm.sh

# My custom MOTD
$HOME/random-cowsay-fortune.sh

Install powerlevel10k

$ git clone --depth=1 https://github.com/romkatv/powerlevel10k.git $ZSH_CUSTOM/themes/powerlevel10k

Update Z Shell configuration for powerlevel10k

You need to modify the ZSH_THEME variable in your Z Shell configuration to use the powerlevel10k theme. After which the start of my ~/.zshrc file looks like:

export ZSH="$HOME/.oh-my-zsh"

ZSH_THEME=powerlevel10k/powerlevel10k

...

Configure powerlevel10k

When you next open a terminal window, the powerlevel10k configuration script will run. If it prompts you to install a font and/or restart iTerm2, then do so. It will then prompt you with a number of questions about how you want the shell's prompt to visually appear. After which it will append the line [[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh to your ~./zshrc for you.

Just for my own future reference, the answers I gave to the questions of the configuration script are recorded in the top of my ~/.p10k.zsh file:

# Generated by Powerlevel10k configuration wizard on 2019-11-29 at 12:18 CET.
# Based on romkatv/powerlevel10k/config/p10k-rainbow.zsh, checksum 20931.
# Wizard options: nerdfont-complete + powerline, small icons, rainbow, round separators,
# sharp heads, flat tails, 1 line, sparse, many icons, concise, transient_prompt.

...

Conclusion

Based on the settings I chose, my iTerm2 terminal prompt now looks rather nice IMHO.

A new Terminal shell
Listing directory contents of a Git repository; prompt shows the git branch and status.

My custom MOTD script

In case anyone is interested, I have included my MOTD script random-cowsay-fortune.sh below. It requires you to first install cowsay, fortune, and lolcat via Homebrew.

#!/usr/bin/env bash

set -e

file=$( ls /usr/local/Cellar/cowsay/3.04/share/cows/*.cow | sort -R | tail -1 )

/usr/local/Cellar/fortune/9708/bin/fortune  | cowsay -f "$file" | lolcat
random-cowsay-fortune.sh