Zsh Configuration Patterns
File Loading Order
Zsh startup files (in order)
# Login shell: .zshenv → .zprofile → .zshrc → .zlogin
# Interactive: .zshenv → .zshrc
# Script: .zshenv only
# Logout: .zlogout
# .zshenv — always loaded. PATH, env vars. Keep minimal.
# .zprofile — login shell only. Like .bash_profile.
# .zshrc — interactive shells. Aliases, functions, completion, prompt.
# .zlogin — after .zshrc for login shells. Rarely used.
# .zlogout — on logout. Cleanup tasks.
Essential Options
setopt patterns
# Directory navigation
setopt AUTO_CD # type dir name to cd into it
setopt AUTO_PUSHD # cd pushes to dir stack
setopt PUSHD_IGNORE_DUPS # no dupes on dir stack
setopt PUSHD_SILENT # don't print stack on pushd
# Globbing
setopt EXTENDED_GLOB # #, ~, ^ in patterns
setopt GLOB_DOTS # include dotfiles in globs
setopt NULL_GLOB # no error on no matches
# Correction
setopt CORRECT # command spell correction
setopt CORRECT_ALL # argument spell correction
# Safety
setopt NO_CLOBBER # don't overwrite with > (use >|)
setopt RM_STAR_WAIT # 10s wait before rm *
# Job control
setopt NO_HUP # don't kill bg jobs on exit
setopt LONG_LIST_JOBS # verbose job listing
Aliases and Functions
Alias patterns
# Simple aliases
alias ll='ls -lah --color=auto'
alias ..='cd ..'
alias ...='cd ../..'
# Git aliases
alias gs='git status'
alias gd='git diff'
alias gp='git push'
# Suffix aliases (open by extension)
alias -s md=nvim
alias -s json=jq
alias -s log='less +F'
# Global aliases (expand anywhere in command)
alias -g G='| grep'
alias -g L='| less'
alias -g H='| head -20'
alias -g C='| wc -l'
alias -g J='| jq .'
# Usage: ps aux G ssh C
# Expands to: ps aux | grep ssh | wc -l
Function patterns
# Auto-loaded functions
# Place in $fpath directory (e.g., ~/.zsh/functions/)
# File name = function name, no function wrapper needed
# Inline functions
mkcd() {
mkdir -p "$1" && cd "$1"
}
# Git commit with heredoc
gach() {
git add -A && git commit -m "$(cat)"
}
# Quick HTTP server
serve() {
python3 -m http.server "${1:-8000}"
}
Environment and PATH
Path management
# Deduplicated path (zsh feature)
typeset -U PATH path
# Add to path (zsh array syntax)
path=(
"$HOME/.local/bin"
"$HOME/.cargo/bin"
"$HOME/go/bin"
$path
)
# Or traditional
export PATH="$HOME/.local/bin:$PATH"
# Conditional path
[[ -d "$HOME/.cargo/bin" ]] && path+=("$HOME/.cargo/bin")
Conditional Loading
Guard patterns
# Check if command exists
(( $+commands[nvim] )) && alias vim=nvim
(( $+commands[eza] )) && alias ls=eza
# Alternative
command -v rg &>/dev/null && alias grep=rg
# Platform-specific
case "$(uname)" in
Linux) alias open=xdg-open ;;
Darwin) alias ll='ls -lah -G' ;;
esac
# Load only if file exists
[[ -f ~/.zsh/secrets.zsh ]] && source ~/.zsh/secrets.zsh