Many developers rave about Oh My Zsh, an open-source, community-driven framework built on top of the zsh shell and playing around with Oh My Zsh has been on my bucket list for some time.
Starting with the Catalina update, zsh is the default shell on Macs and WSL 2 became available on Windows 10 in 2020 (and a native Linux GUI is apparently coming to Windows soon), so it seemed like a good time to dig deeper and learn how to use zsh shell.
Install Zsh Shell and Oh My Zsh – MacOS
Install Zsh Shell – MacOS
Oh My Zsh requires that the zsh shell be installed. Zsh is already installed on MacOS. If you are using bash as your default shell, you can experiment with zsh and Oh My Zsh without changing your default settings. Open the terminal, click Terminal -> Preferences and for the setting “Shells open with”, select the option “Command (complete path)” and enter “/bin/zsh”.
Close and launch a new Terminal window. The shell should be zsh.
You will be prompted to update your default shell to zsh.
Once you are happy with your settings in the zsh shell, run the following command to set your default
chsh -s /bin/zsh
Alternatively, you can use a different terminal such as iTerm2 and configure it to use zsh by default. I found this to be a good way to get familiar with zsh while still having bash as the default in the Mac terminal.
Install Oh My Zsh
Next download and install Oh My Zsh using the command below:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
Close the terminal and reopen it. The terminal should start up with the default Oh My Zsh theme.
Install Zsh Shell and Oh My Zsh – Windows 10
Before you begin, make sure you have enabled the Windows Subsystem for Linux. I’m using Ubuntu running on WSL 2 in this example.
Install Zsh – Ubuntu on WSL 2
Open a terminal window and install zsh and make it your default shell.
sudo apt install zsh
//type "y" to accept and then check that it was installed correctly
zsh --version
//make zsh your default shell
chsh -s $(which zsh)
Launch Zsh and go through through the first-time setup process.
zsh
// Select option (2) to start with the default configuration
2
Install Oh My Zsh
The installation command are the same for Ubuntu and MacOS.
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
Install Fonts and Ligatures
Add Fonts
Oh My Zsh comes with a number of custom themes to choose from, but many of them rely on fonts that are usually not already installed on your local machine. One font that is used in a number of themes is Powerline fonts.
Install the Powerline Fonts – MacOS
git clone https://github.com/powerline/fonts.git
cd fonts
./install.sh
After installing the fonts, you’ll need to update your terminal application for use the fonts. See instructions for updating fonts in Mac Terminal and iTerm2.
Install the Powerline Fonts – Windows
To use Powerline font in WSL, you install the fonts in the Windows OS. Clone the Powerline fonts repo to a folder in the Windows OS and open PowerShell as an administrator to execute the “install.ps1” script
After installing the fonts, update the Windows Terminal to use the fonts. Open the settings JSON file for the terminal and add the following line to the profile for Ubuntu on WSL 2.
"fontFace": "DejaVu Sans Mono for Powerline"
// don't forget to add the "," to the line above to maintain correct JSON syntax!
Ligatures
Some terminals, such as iTerm2 and Windows Terminal also support ligatures. In order to enable ligatures, you will need to install a font that can support these types of characters, such as FiraCode and Cascadia Code. Install FiraCode or Cascadia Code and then update your terminal settings to use the font.
Customize Oh My Zsh
Update Your Theme
Oh My Zsh comes with a number of great themes preinstalled. To activate one, simply update your zsh profile file.
nano ~/.zshrc
# set ZSH_THEME variable to name of theme, for example "agnoster"
# save and close .zshrc file
source ~/.zshrc
Agnoster is a great theme. The one complaint I have is that the prompt is quite long since it shows the computer/user name and the full path to the current directory. You can add the following to your .zshrc file to shorten the prompt. Make sure you add the following after the line: source $ZSH/oh-my-zsh.sh
# Shorten path displayed in prompt to current and parent directory
prompt_dir () {
prompt_segment blue $CURRENT_FG '%2~'
}
# Suppress computer and user name from prompt
prompt_context () { }
There are also a number of external themes that you can install if you want more options than the themes included by default. A list of external themes along with with installation instructions can be found here – https://github.com/ohmyzsh/ohmyzsh/wiki/External-themes. Some of my favorites are:
- Spaceship https://github.com/denysdovhan/spaceship-prompt
- Power Level 10k https://github.com/romkatv/powerlevel10k
Install Plugins
Plugins add more functionality to Oh My Zsh, such as suggestions and auto-completion. A list of all official plugins https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins. Try to just install the plugins you need, otherwise it will take a long time to load the zsh shell.
The git plugin is included by default. Some plugins can be added simply by updating the .zshrc file and adding the plugin name to the plugins=(…) list.
Some plugins require more setup steps. For example the VS Code plugin will only work after you update PATH variable and add the plugin name to the plugins=(…) list.
There are many more plugins aside from the official list. One of my favorites is the salesforce-cli-zsh-completion plugin, which is more current than the sfdx plugin included in the official list.
Resources
- Installing Zsh: Instructions for various operating systems
- iTerm2: Enhance iTerm2 color schemes
- Windows Terminal: Installing Additional Color Schemes