Setting up Zsh with Oh-My-Zsh

Set up beautiful terminal using Zsh with Oh-My-Zsh and install custom theme and plugins


Before Starting

Supported Installation

This installation procedure is tested on Ubuntu 22.04.x LTS with x86_64 architecture, but should be compatible with other Linux distributions and macOS.

Installation

Install dependencies

sudo apt install vim curl git

It's recommended to install Nerd Font in this step to avoid display problems.
If Nerd Font isn't properly installed, you will not be able to experience the full functionality of Powerlevel10k.

  1. Install dependencies:
    • sudo apt install fontconfig
  2. Download and install one of the nerd font:
  3. Install fonts:
    • Move all fonts to /usr/local/share/fonts/TTF
    • If the directory doesn't exist, create one
    • Rebuild the font cache with fc-cache -f -v

Install Zsh

sudo apt install zsh

Set Zsh as default shell

sudo chsh -s $(which zsh)

Install On My Zsh

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

Install Powerlevel10k

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

Switch the theme to Powerlevel10k by editing the config file which located in /home/$USER/.zshrc (~/.zshrc).
Change the line ZSH_THEME="robbyrussell" to ZSH_THEME="powerlevel10k/powerlevel10k".

Apply the changes by executing source /home/$USER/.zshrc (source ~/.zshrc).

The Powerlevel10k configuration wizard should be started automatically.
If it doesn't, execute p10k configure to start the configuration wizard.
Choose your favorites configuration!

My personal favorite configs are: 3121132342221y1y (Start from "Prompt Style")

Install plugins (Optional)

Zsh-Completions
Additional completion definitions for Zsh.

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

Zsh-Autosuggestions
It suggests commands as you type based on history and completions.

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

Zsh-Syntax-Highlighting
It enables highlighting of commands whilst they are typed at a zsh prompt into an interactive terminal.

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

Finally, add the following configuration to .zshrc:

plugins=(
  git
  zsh-completions 
  zsh-autosuggestions 
  zsh-syntax-highlighting
)

Apply the changes by executing source /home/$USER/.zshrc (source ~/.zshrc).

Finishing

Enjoy your perfect terminal with fantastic looking, autosuggestions and syntax-highlighting~

Little tip

Fix prompt at the bottom

Add the following lines to the end of .zshrc file to anchor the prompt to the bottom of the terminal window.

alias _clear=$(which clear)
clear() {
  _clear
  printf '\n%.0s' {1..$(($(tput lines) - 2))}
}
printf '\n%.0s' {1..$(($(tput lines) - 2))}

The original "clear" command is now called _clear.

Warp terminal patch

If you are using Warp terminal with powerlevel10k multi-line prompt, you may face the issue of the prompt and input on different lines.

To fix the issue, create a seperate configuration file that uses powerlevel10k one-line prompt at home directory and name it .p10k-warp.zsh.

Finally, replace the line [[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh in .zshrc with the following one.

.zshrc
if [[ $(printenv | grep WARP) ]];
then
	[[ ! -f ~/.p10k-warp.zsh ]] || source ~/.p10k-warp.zsh
else
	[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
fi

Don't forget to run source ~/.zshrc to apply the changes.