Skip to main content

 

Z shell (Zsh) is a Unix shell built on top of bash (the default shell for macOS) with a large number of improvements.

In this article, I'll run through how to configure iTerm2 with ZSH and its dependencies.

 

Step 1: Install Homebrew

Homebrew is a free and open-source software package management system that simplifies the installation of software on Apple’s macOS.

Before installing Homebrew, we need to install the CLI tools for Xcode. Open your terminal and run the command:

xcode-select —-install

If you get an error, run xcode-select -r to reset xcode-select.  On my first run I received the following error

xcode-select —-install
xcode-select: error: invalid argument '—-install'
Usage: xcode-select [options]

Print or change the path to the active developer directory. This directory
controls which tools are used for the Xcode command line tools (for example, 
xcodebuild) as well as the BSD development commands (such as cc and make).

Options:
  -h, --help                  print this help message and exit
  -p, --print-path            print the path of the active developer directory
  -s <path>, --switch <path>  set the path for the active developer directory
  --install                   open a dialog for installation of the command line developer tools
  -v, --version               print the xcode-select version
  -r, --reset                 reset to the default command line tools path

Then running 

xcode-select -r

Which produced

xcode-select: error: --reset must be run as root (e.g. `sudo xcode-select --reset`).

You skip this by running the command sudo su, then the command or as suggested

sudo xcode-select --reset

Okay now you can install Homebrew.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

 

Step 2: Install iTerm2

iTerm2 is a replacement for terminal and the successor to iTerm. Most software engineers prefer iTerm2 over the default terminal that ships with macOS as a result of its cool features. You can integrate zsh into iTerm2 to increase productivity.

To install iTerm2, run the command:

brew install iterm2

 

Step 3: Install ZSH

Zsh is a shell designed for interactive use, although it is also a powerful scripting language.

By default, macOs ships with zsh located in/bin/zsh.

Let’s install zsh using brew and make iTerm2 use it.

brew install zsh

 

Step 4: Install Oh My Zsh

It runs on Zsh to provide cool features configurable within the ~/.zhrc config file. Install Oh My Zsh by running the command

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Check the installed version

zsh --version

You can upgrade it to get the latest features it offers.

omz update

Note this command used to be upgrade_oh_my_zsh, however `upgrade_oh_my_zsh` is deprecated. Use `omz update` instead.

Restart iTerm2 to dive into the new experience of using Zsh. Welcome to the “Oh My Zsh” world ?.

That’s not all. Now, we will install the dependencies to get the best out of Zsh.

 

Step 5: Change the Default Theme

Oh My Zsh comes bundled with a lot of themes. The default theme is robbyrussell, but you can change it to any theme of your choice. In this scenario, I changed it to agnoster, an already pre-installed theme.

You then need to select this theme in your ~/.zshrc. To open the config file (.zshrc), run the command:

nano ~/.zshrc

Or open the file in a text editor with

open ~/.zshrc

Set the zsh theme and update your changes

source ~/.zshrc

Using a Custom Theme

To install another theme not pre-installed, clone the repository into custom/themesdirectory. In this scenario, we’ll install powerlevel9k,

git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k

Or install powerlevel10k,

git clone https://github.com/romkatv/powerlevel10k.git ~/.oh-my-zsh/custom/themes/powerlevel10k

or an even better solution and easy to configure is to run 

brew install romkatv/powerlevel10k/powerlevel10k
echo "source $(brew --prefix)/opt/powerlevel10k/powerlevel10k.zsh-theme" >>~/.zshrc

Then, select this theme in your ~/.zshrc

ZSH_THEME="powerlevel10k/powerlevel10k"

Update your changes by running the command

source ~/.zshrc

Navigate to iTerm2 > Preferences > Profiles > Colors if you wish to change the background color of the terminal.

The selected theme in this scenario requires powerline fonts. You guessed it, we now need to install that.

 

Step 6: Install Fonts

I will be using Inconsolata. Get your preferred font out of these powerline fonts. Then, download and install it.

Or download the entire font.

git clone https://github.com/powerline/fonts.git

cd fonts

./install.sh

To change the font, navigate to iTerm2 > Preferences > Profiles > Text > Change Font.

Now, you can see Inconsolata listed as one of the fonts. Select your preferred font. For fonts that support ligatures like FiraCode, check the “Use ligatures” option to view your arrows and other operators in a stylish manner like ( → ).
Select a powerline font

 

Step 7: Install Color Scheme

Let’s change the color scheme to bring out the beauty of our terminal. Navigate to iTerm2-Color-Schemes and download the ZIP folder. Then, extract the downloaded folder cos what we need resides in the schemes folder.

Navigate to iTerm2 > Preferences > Profile > Colors > Color Presets > Import

    Navigate to the schemes folder and select your preferred color schemes to import them.
    Click on a specific color scheme to activate it. In this scenario, I activated Batman which is my preferred color scheme.

Okay - well done, we are down with the basic settings.

 

Step 8: Install Plugins
Oh My ZSH

Oh My ZSH comes preloaded with a git plugin. To add more, for instance, docker, auto-suggestion, syntax highlighting and more:

Clone the Git repository

git clone https://github.com/zsh-users/zsh-docker.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-docker

Head over to .oh-my-zsh > custom > plugins directory to view the cloned directory. To access this, run the command

open ~/.oh-my-zsh

Add the plugin to the plugin section of the config file ~/.zshrc shown below

Update your changes by running the command

source ~/.zshrc

 

zsh-autosuggestions

Another very useful plugin is zsh-autosuggestions. Every time you type something, you get a suggestion in grey. Press “tab” key to get even more suggestions (e.g. options of the command).

Run the following command to install this plugin:

git clone https://github.com/zsh-users/zsh-autosuggestions.git $ZSH_CUSTOM/plugins/zsh-autosuggestions

Then open your ~/.zshrc file, add zsh-autosuggestions:

plugins=(
  # other plugins
  zsh-autosuggestions
)

Close the shell and open a new session, it works instantly.

 

zsh-syntax-highlighting

The zsh-syntax-highlighting plugin highlights your command in red if it’s incorrect, on the other hand it highlights in green if it’s correct. To install it, run:

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_CUSTOM/plugins/zsh-syntax-highlighting

Add it to ~/.zshrc:

plugins=(
  # other plugins
  zsh-syntax-highlighting
)

 

Step 9: Add Aliases

Aliases are shortcuts used to reduce the time spent on typing commands. Add aliases to commands you run in the section shown below.

Related articles

Andrew Fletcher06 May 2024
Exploring historical memory usage monitoring with Sysstat
In the realm of system administration and monitoring, understanding memory usage trends is crucial for maintaining system health and performance. While tools like htop offer real-time insights into memory usage, they lack the capability to provide historical data. So, what if you need to analyze...