Skip to content

Zsh and Oh My Zsh Installation & Configuration Guide

Introduction

Zsh (Z Shell) is a powerful shell with features like improved tab completion, spelling correction, and theming capabilities. Oh My Zsh is a framework for managing Zsh configuration, providing hundreds of plugins and themes to enhance your terminal experience.

Installing Zsh

Ubuntu/Debian

bash
sudo apt update
sudo apt install zsh

CentOS/RHEL

bash
sudo yum install zsh

macOS

bash
# Using Homebrew
brew install zsh

# macOS comes with zsh by default since Catalina

Windows

bash
# Using Windows Subsystem for Linux (WSL)
# First install WSL with Ubuntu, then:
sudo apt update
sudo apt install zsh

Verify Installation

bash
zsh --version

Setting Zsh as Default Shell

bash
chsh -s $(which zsh)

You'll need to log out and log back in for the change to take effect.

Installing Oh My Zsh

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

# Using wget
sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"

Configuring Oh My Zsh

The main configuration file for Zsh is ~/.zshrc. Oh My Zsh will create this file automatically.

Changing Themes

Oh My Zsh comes with many themes. To change your theme, edit ~/.zshrc and change the ZSH_THEME line:

bash
# Set theme to "robbyrussell" (default)
ZSH_THEME="robbyrussell"

# Other popular themes
# ZSH_THEME="agnoster"
# ZSH_THEME="powerlevel10k/powerlevel10k"
# ZSH_THEME="spaceship"

To install additional themes like Powerlevel10k:

bash
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

Then set ZSH_THEME="powerlevel10k/powerlevel10k" in your ~/.zshrc.

Installing Plugins

Oh My Zsh comes with many plugins. To enable plugins, edit your ~/.zshrc file:

bash
# Default plugins
plugins=(git)

# Enhanced set of plugins
plugins=(
  git
  zsh-autosuggestions
  zsh-syntax-highlighting
  z
  docker
  docker-compose
  npm
  history
  sudo
)

To install additional plugins:

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

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

Setting up Aliases

Oh My Zsh includes many useful aliases. You can add your own to ~/.zshrc:

bash
# Example aliases
alias zshconfig="nano ~/.zshrc"
alias ohmyzsh="nano ~/.oh-my-zsh"
alias ll="ls -la"
alias update="sudo apt update && sudo apt upgrade"

Customization Tips

Custom Functions

You can add custom functions to your ~/.zshrc:

bash
# Create a directory and navigate to it
mkcd() {
  mkdir -p "$1" && cd "$1"
}

# Extract various archive formats
extract() {
  if [ -f $1 ]; then
    case $1 in
      *.tar.bz2) tar xjf $1 ;;
      *.tar.gz) tar xzf $1 ;;
      *.bz2) bunzip2 $1 ;;
      *.rar) unrar e $1 ;;
      *.gz) gunzip $1 ;;
      *.tar) tar xf $1 ;;
      *.tbz2) tar xjf $1 ;;
      *.tgz) tar xzf $1 ;;
      *.zip) unzip $1 ;;
      *.Z) uncompress $1 ;;
      *) echo "'$1' cannot be extracted via extract()" ;;
    esac
  else
    echo "'$1' is not a valid file"
  fi
}

Environment Variables

Set up environment variables in your ~/.zshrc:

bash
# Example environment variables
export PATH=$HOME/bin:/usr/local/bin:$PATH
export EDITOR='vim'

Applying Configuration Changes

After making changes to your ~/.zshrc file, you need to reload the configuration for changes to take effect. You can do this without restarting your terminal:

bash
# Option 1: Using source command
source ~/.zshrc

# Option 2: Using shorthand notation
. ~/.zshrc

# Option 3: Using Oh My Zsh alias that's predefined
omz reload

Some changes, particularly those involving plugins or themes that weren't previously installed, might require restarting your terminal session for full effect.

Font Installation for Powerline Themes

Many themes use Powerline symbols. Install a patched font:

  1. Download a Nerd Font from https://www.nerdfonts.com/
  2. Install the font on your system
  3. Configure your terminal emulator to use the Nerd Font

Common Issues and Troubleshooting

Broken Themes

If your theme appears broken with unknown characters:

  • Ensure you've installed a compatible font
  • Set your terminal to use this font

Slow Startup

If Zsh is slow to start:

  • Reduce the number of enabled plugins
  • Use zprof to profile your startup time

Reset to Default

To reset Oh My Zsh to default:

bash
rm ~/.zshrc
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc

Updating Oh My Zsh

bash
# Automated update
omz update

# Manual update
cd ~/.oh-my-zsh && git pull

Restart Zsh

bash
exec zsh

Useful Resources