Setting Up Zsh Terminal and Configuring It with Powerlevel10k

Step 1: Install Zsh

  1. Check if Zsh is already installed:

     zsh --version
    
  2. Install Zsh if it’s not installed:

    • MacOS (using Homebrew):

        brew install zsh
      
    • Ubuntu/Debian:

        sudo apt install zsh -y
      
    • Fedora:

        sudo dnf install zsh -y
      
  3. Set Zsh as the default shell:

     chsh -s $(which zsh)
    
  4. Restart your terminal to use Zsh.


Step 2: Install Oh My Zsh

Oh My Zsh is a framework for managing your Zsh configuration.

  1. Install Oh My Zsh:

     sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
    
  2. Verify Installation: After installation, you’ll notice your terminal prompt changes, indicating Oh My Zsh is active.


Step 3: Install and Configure Powerlevel10k

Powerlevel10k is a popular theme for Zsh, offering a highly customizable and visually appealing prompt.

  1. Install Powerlevel10k:

     git clone --depth=1 https://github.com/romkatv/powerlevel10k.git $ZSH_CUSTOM/themes/powerlevel10k
    
  2. Set Powerlevel10k as the Theme: Open your .zshrc file:

     nano ~/.zshrc
    

    Update the ZSH_THEME line:

     ZSH_THEME="powerlevel10k/powerlevel10k"
    
  3. Apply the Changes:

     source ~/.zshrc
    
  4. Run Configuration Wizard: Customize the theme using:

     p10k configure
    

Step 4: Install Zsh Plugins

Plugins enhance Zsh by adding helpful functionalities. Here are some essential plugins to install:

  1. Install Plugins:

    • 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
      
  2. Enable Plugins in .zshrc: Add these plugins to the plugins array:

     plugins=(
       git
       zsh-autosuggestions
       zsh-syntax-highlighting
       jsontools
       history
       dirhistory
       vscode
       docker
       kubectl
       terraform
       aws
       pip
       python
       npm
     )
    
  3. Apply Changes:

     source ~/.zshrc
    

Step 5: Add Custom Paths and Environment Variables

Customize your .zshrc to include paths for tools and frameworks you frequently use:

# Java
export JAVA_HOME=`/usr/libexec/java_home -v 17`

# Flutter
export PATH="$HOME/development/flutter/bin:$PATH"

# Android SDK
export ANDROID_HOME="$HOME/Library/Android/sdk"
export PATH="$ANDROID_HOME/platform-tools:$ANDROID_HOME/emulator:$PATH"

# MySQL
export PATH="/usr/local/mysql-8.0.36-macos14-arm64/bin:$PATH"

# Node.js
export NODE_PATH=$(which node)

Step 6: iTerm2 Integration (Optional)

If you’re using iTerm2, enable shell integration for an enhanced experience:

curl -L https://iterm2.com/shell_integration/zsh -o ~/.iterm2_shell_integration.zsh
source ~/.iterm2_shell_integration.zsh

Step 7: Backup and Reload Configurations

Create shortcuts for backing up and reloading your .zshrc file:

# Backup .zshrc
alias backup-zshrc="cp ~/.zshrc ~/.zshrc.backup && echo 'Backup created at ~/.zshrc.backup'"

# Reload .zshrc
alias update-zsh="source ~/.zshrc && echo 'Reloaded .zshrc settings'"

Step 8: Final Touch - Custom Aliases and Functions

Create custom aliases or organize them into a directory for easier management:

  1. Add Aliases in .zshrc:

     alias ll="ls -lah"
     alias gs="git status"
     alias py="python3"
    
  2. Load Aliases from Separate Files:

     if [ -d "$HOME/.alias" ]; then
         for file in $HOME/.alias/*; do
             [ -f "$file" ] && source "$file"
         done
     fi
    

Step 9: Regular Updates

Keep Zsh, Oh My Zsh, and plugins up to date:

  1. Update Zsh:

     brew upgrade zsh
    
  2. Update Oh My Zsh and Plugins:

     omz update
    

Sample .zshrc File

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
    source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# Suppress Powerlevel10k instant prompt warnings
# typeset -g POWERLEVEL9K_INSTANT_PROMPT=quiet

# Path to your oh-my-zsh installation
export ZSH="$HOME/.oh-my-zsh"

# Theme
ZSH_THEME="powerlevel10k/powerlevel10k"

# Enable command auto-correction
ENABLE_CORRECTION="true"

# Display red dots whilst waiting for completion
COMPLETION_WAITING_DOTS="true"

# Plugins
plugins=(
  git
  zsh-autosuggestions
  zsh-syntax-highlighting
  history
  vscode
  kubectl
)

# Load Oh My Zsh
source $ZSH/oh-my-zsh.sh

# Custom Settings

# Java Home
export JAVA_HOME=$(/usr/libexec/java_home -v 17)

# Vim Keybindings
bindkey -v

# Load Environment Variables from `.env` Directory
if [ -d "$HOME/.env" ] && [ "$(ls -A $HOME/.env)" ]; then
    for file in $HOME/.env/*.env; do
        [ -f "$file" ] && . "$file"
    done
fi

# Load Aliases from `.alias` Directory
if [ -d "$HOME/.alias" ] && [ "$(ls -A $HOME/.alias)" ]; then
    for file in $HOME/.alias/*.sh; do
        [ -f "$file" ] && . "$file"
    done
fi

# Additional Path Updates (already moved to system-paths.env)
export PATH="$PATH"

# Enable NVM (Node Version Manager)
if [ -d "$HOME/.nvm" ]; then
    export NVM_DIR="$HOME/.nvm"
    if [ -s "$NVM_DIR/nvm.sh" ]; then
        source "$NVM_DIR/nvm.sh"
    fi
fi

# iTerm2 Shell Integration
if [ -e "$HOME/.iterm2_shell_integration.zsh" ]; then
    source "$HOME/.iterm2_shell_integration.zsh"
fi

# Backup and Update Aliases
alias backup-zshrc="cp ~/.zshrc ~/.zshrc.backup && echo 'Backup created at ~/.zshrc.backup'"
alias update-zsh="source ~/.zshrc && echo 'Reloaded .zshrc settings'"

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

Organizing Aliases and Environment Variables

To keep your configuration organized, you can separate your aliases and environment variables into dedicated directories:

  • Environment Variables: Store your environment variables in the ~/.env directory. Each file should have a .env extension and contain the necessary export statements.

  • Aliases: Store your aliases in the ~/.alias directory. Each file should have a .sh extension and contain the alias definitions.

This organization helps maintain a clean and manageable .zshrc file, making it easier to update and troubleshoot.

Conclusion

With Zsh and Powerlevel10k configured, you now have a powerful terminal setup tailored to your workflow. This setup is highly customizable, so feel free to tweak it further as needed. Happy coding! 🚀

0
Subscribe to my newsletter

Read articles from Himanshu Nikhare directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Himanshu Nikhare
Himanshu Nikhare

I am a Senior Software Engineer at BigBasket with over three years of experience in software development. Specializing in QA automation, CI/CD, and mobile automation, I am passionate about creating efficient workflows and sharing knowledge through blogs and articles. I actively contribute to the tech community through my work, open-source projects, and insightful content. Feel free to connect with me on GitHub or LinkedIn to explore my projects and contributions.