Zsh Shell Guide
Zsh (Z Shell) is a powerful shell that extends bash with many additional features. It’s especially popular thanks to Oh My Zsh, a community-driven framework for managing Zsh configuration.
Key Features
Section titled “Key Features”Enhanced Completion
- Intelligent tab completion
- Cursor-based menu selection
- Expandable abbreviations
Plugins & Themes
- Oh My Zsh framework
- Thousands of plugins
- Hundreds of themes
Advanced Features
- Shared history between sessions
- Globbing improvements
- Spelling correction
Installation
Section titled “Installation”macOS
# Install via Homebrewbrew install zsh
# Set as default shellchsh -s /bin/zshUbuntu/Debian
sudo apt install zsh
# Set as defaultchsh -s /usr/bin/zshVerify
echo $SHELLOh My Zsh
Section titled “Oh My Zsh”Installation
Section titled “Installation”sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"Configuration File
Section titled “Configuration File”- Location:
~/.zshrc
Update Oh My Zsh
Section titled “Update Oh My Zsh”omz updateConfiguration Files
Section titled “Configuration Files”| File | Purpose |
|---|---|
~/.zshrc |
Main configuration file |
~/.zprofile |
Login shell config |
~/.zshenv |
Always sourced |
~/.zlogin |
After .zprofile |
Essential Plugins
Section titled “Essential Plugins”Enable Plugins
Section titled “Enable Plugins”# Edit ~/.zshrcplugins=( git docker python pip kubectl zsh-autosuggestions zsh-syntax-highlighting)Recommended Plugins
Section titled “Recommended Plugins”zsh-autosuggestions
- Suggests commands as you type
- Based on history
- Install:
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
zsh-syntax-highlighting
- Syntax highlighting for commands
- Red for invalid, green for valid
- Install:
git clone https://github.com/zsh-users/zsh-syntax-highlighting ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
z
- Quick directory jumping
- Based on frecency (frequent + recent)
Useful Built-in Plugins
Section titled “Useful Built-in Plugins”| Plugin | Description |
|---|---|
| git | Git aliases and functions |
| docker | Docker completions |
| kubectl | Kubernetes completions |
| pip | Python pip completions |
| python | Python virtualenv support |
| npm | npm completions |
| brew | Homebrew completions |
Aliases
Section titled “Aliases”# Shortcutsalias ll='eza -lah'alias la='eza -a'alias l='eza -l'
# Gitalias gs='git status'alias ga='git add'alias gc='git commit'alias gp='git push'alias gco='git checkout'
# Navigationalias ..='cd ..'alias ...='cd ../..'
# Safetyalias rm='rm -i'alias cp='cp -i'alias mv='mv -i'Functions
Section titled “Functions”# Create and cd into directoryfunction mkcd() { mkdir -p "$1" && cd "$1"}
# Extract archivesextract() { if [ -f "$1" ] ; then case "$1" in *.tar.bz2) tar xjf "$1" ;; *.tar.gz) tar xzf "$1" ;; *.bz2) bunzip2 "$1" ;; *.rar) unrar x "$1" ;; *.gz) gunzip "$1" ;; *.tar) tar xf "$1" ;; *.tbz2) tar xjf "$1" ;; *.tgz) tar xzf "$1" ;; *.zip) unzip "$1" ;; *.7z) 7z x "$1" ;; *) echo "'$1' cannot be extracted" ;; esac else echo "'$1' is not a valid file" fi}Themes
Section titled “Themes”Using a Theme
Section titled “Using a Theme”# In ~/.zshrcZSH_THEME="robbyrussell"Popular Themes
Section titled “Popular Themes”| Theme | Description |
|---|---|
| robbyrussell | Default, simple and clean |
| agnoster | Powerline-style, requires font |
| powerlevel10k | Highly customizable, popular |
| starship | Fast, cross-shell |
Powerlevel10k
Section titled “Powerlevel10k”# Installgit clone --depth=1 https://github.com/romkatv/powerlevel10k ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/themes/powerlevel10k
# Set in ~/.zshrcZSH_THEME="powerlevel10k/powerlevel10k"
# Configurep10k configureUseful Shortcuts
Section titled “Useful Shortcuts”| Shortcut | Action |
|---|---|
Ctrl+A |
Move to beginning |
Ctrl+E |
Move to end |
Ctrl+U |
Clear line before |
Ctrl+K |
Clear line after |
Ctrl+R |
Search history |
Ctrl+O |
Accept and run |
Alt+. |
Insert last argument |
Esc+Q |
Push current line to stack |
Ctrl+X,G |
Insert all completions |
Environment Variables
Section titled “Environment Variables”# In ~/.zshrcexport PATH="$HOME/bin:$PATH"export EDITOR=vimexport VISUAL=vim
# History settingsexport HISTSIZE=10000export SAVEHIST=10000export HISTFILE=~/.zsh_historyAdvanced Features
Section titled “Advanced Features”Globbing
Section titled “Globbing”# Recursive searchls **/*.txt
# Case-insensitivels -l (**).txtHistory
Section titled “History”# Shared history (enable in ~/.zshrc)setopt SHARE_HISTORYsetopt HIST_IGNORE_DUPSSpelling Correction
Section titled “Spelling Correction”# Enablesetopt CORRECTsetopt CORRECT_ALLUseful Options
Section titled “Useful Options”# In ~/.zshrcsetopt AUTO_CD # cd by typing directorysetopt AUTO_PUSHD # Push directory to stacksetopt AUTO_LIST # Automatically list choicessetopt MENU_COMPLETE # Auto-insert first matchsetopt HIST_IGNORE_ALL_DUPS # No duplicate commandssetopt SHARE_HISTORY # Share history between sessionsTips & Tricks
Section titled “Tips & Tricks”Faster completions
Section titled “Faster completions”# In ~/.zshrczstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'Colorize completions
Section titled “Colorize completions”zstyle ':completion:*' menu selectDisable automatic updates
Section titled “Disable automatic updates”DISABLE_AUTO_UPDATE="true"