Complete Zsh Cheatsheet

Powerful features and shortcuts for the Z Shell

Quick Reference

Zsh is a powerful shell with advanced features like improved tab completion, globbing, and theming.

cd -
Toggle between current and previous directory
$ cd /var/www
$ cd /etc/nginx
$ cd - # Returns to /var/www
$ cd - # Returns to /etc/nginx
cdpath=(. ~/projects)
Set base directories for cd command
cdpath=(. ~/projects ~/work)
cd project1 # Changes to ~/projects/project1
setopt autocd
Change to directory by typing its name without cd
setopt autocd
/etc # Changes to /etc directory
~ # Changes to home directory

2 Completion

autoload -Uz compinit && compinit
Initialize completion system
# Add to .zshrc to enable completion
autoload -Uz compinit
compinit
zstyle ':completion:*' menu select
Enable menu selection for completions
# Add to .zshrc for interactive selection
zstyle ':completion:*' menu select
Tab completion
Zsh's powerful tab completion features
ssh # Press tab to show hosts from ~/.ssh/config
kill # Press tab to show running processes
git checkout # Press tab to show branches
setopt correct
Enable command correction
setopt correct
sl # Zsh suggests: correct 'sl' to 'ls' [nyae]?
setopt complete_in_word
Complete in the middle of words
setopt complete_in_word
/usr/local/bin/chro # Completes to /usr/local/bin/chromedriver

3 Globbing (File Patterns)

ls **/*.txt
Recursive globbing - match files in all subdirectories
ls **/*.js # All .js files recursively
rm **/*.bak # Delete all .bak files recursively
ls *.txt~important.txt
Pattern negation - match all .txt files except important.txt
cp *.pdf~old.pdf backup/ # Copy all PDFs except old.pdf
ls (foo|bar)*.js
OR patterns - match files starting with foo or bar
ls *(test|spec).js # Files ending with test.js or spec.js
ls file<1-10>.txt
Numeric ranges - match file1.txt through file10.txt
mv photo<1-5>.jpg album/ # Move photo1.jpg to photo5.jpg
setopt extended_glob
Enable extended globbing patterns
setopt extended_glob
ls ^*.txt # All files except .txt
ls *.js(#qN) # Null glob - don't error if no matches
ls *(.m-1)
Glob qualifiers - filter by file attributes
ls *(.) # Regular files only
ls *(/) # Directories only
ls *(.m0) # Files modified today
ls **/*(.Lk+100) # Files >100KB recursively

4 Aliases & Functions

alias
Create command shortcuts
alias ll='ls -lh'
alias gs='git status'
alias -g G='| grep' # Global alias
alias -s txt=code # Open .txt files with VS Code
Suffix aliases
Open files based on extension
alias -s js=code
alias -s {pdf,epub}=zathura
script.js # Opens in VS Code
document.pdf # Opens in Zathura
Global aliases
Aliases that can expand
alias -g L='| less'
alias -g G='| grep'
alias -g N='&> /dev/null'
cat large.log L # Pipes to less
ps aux G zsh # Pipes to grep
Functions
Create reusable shell functions
# Simple function
function mkcd() {
  mkdir -p "$1" && cd "$1"
}

# Git function with arguments
function gac() {
  git add . && git commit -m "$1"
}

5 History

history
View and manage command history
history # Show all commands
history 20 # Show last 20 commands
!100 # Execute command #100
!! # Repeat last command
HISTFILE and HISTSIZE
Configure history file location and size
# Add to .zshrc
HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000
setopt appendhistory
setopt share_history # Share history across sessions
history-substring-search
Search history with substring matching
# Enable in .zshrc (requires plugin)
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down

# Usage: Type part of command and press up/down
Ctrl+R
Reverse search through history
# Press Ctrl+R and start typing
(reverse-i-search)`git': git commit -m "update"
# Press Ctrl+R again to cycle through matches
setopt hist_ignore_all_dups
Control how duplicates are stored in history
setopt hist_ignore_all_dups # Ignore duplicates
setopt hist_save_no_dups # Don't save duplicates
setopt hist_reduce_blanks # Remove extra blanks
setopt hist_verify # Show command before executing

6 Prompt Customization

PROMPT variable
Customize your shell prompt
# Simple prompt
PROMPT='%n@%m %~ %# '

# With colors and git info
PROMPT='%F{blue}%n%f@%F{green}%m%f %F{yellow}%~%f %# '
Prompt escape sequences
Special codes for prompt customization
%n # Username
%m # Hostname
%~ # Current directory
%F{color} # Change text color
%? # Exit code of last command
%D{format} # Date/time
RPROMPT
Right-side prompt
# Show time on right
RPROMPT='%D{%L:%M %p}'

# Show git branch on right
autoload -Uz vcs_info
precmd() { vcs_info }
RPROMPT='${vcs_info_msg_0_}'
Theme customization
Predefined prompt themes
# List available themes
prompt -l

# Preview a theme
prompt -p fade

# Set a theme
prompt fade

7 Oh-My-Zsh

Installation
Popular Zsh configuration framework
# Install Oh-My-Zsh
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# Or with wget
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
Plugins
Extend functionality with plugins
# Edit ~/.zshrc
plugins=(
  git
  docker
  zsh-autosuggestions
  zsh-syntax-highlighting
)
Themes
Change the look of your prompt
# Edit ~/.zshrc
ZSH_THEME="agnoster"

# Popular themes:
robbyrussell # Default
agnoster # Powerline-style
af-magic # Minimal
spaceship # Feature-rich

8 Keyboard Shortcuts

Editing shortcuts
Common line editing shortcuts
Ctrl + A # Move to beginning of line
Ctrl + E # Move to end of line
Ctrl + U # Delete to beginning of line
Ctrl + K # Delete to end of line
Ctrl + W # Delete previous word
Ctrl + Y # Paste (yank) deleted text
Navigation shortcuts
Moving around the command line
Alt + B # Move back one word
Alt + F # Move forward one word
Ctrl + XX # Toggle between start and current position
Ctrl + L # Clear screen (same as clear)
Process control
Managing running processes
Ctrl + C # Terminate current command
Ctrl + Z # Suspend current command
Ctrl + D # Exit shell (if line is empty)
Ctrl + R # Reverse history search
Ctrl + G # Abort current operation