Skip to main content

ZSH Configuration

· 3 min read
Max Kaido
Architect

A curated guide to supercharge your shell experience with ZSH and Oh My Zsh

🌟 Top Features

  1. Smart Completion

    • Case-insensitive matching
    • Path completion with highlighting
    • Command history suggestions
    • Fuzzy search capabilities with fzf
  2. Plugin System

    • Git integration
    • Syntax highlighting with advanced patterns
    • Auto-suggestions
    • Directory jumping with fzf
  3. Modern Tools

    • eza for beautiful ls output
    • fzf for fuzzy finding
    • Syntax highlighting with patterns
    • Smart aliases
  4. History Management

    • Shared history across sessions
    • Pattern search with fzf
    • Timestamp tracking
    • Duplicate removal
  5. Directory Navigation

    • Auto cd
    • Directory stack
    • Named directories
    • Path abbreviation

🛠 Essential Configuration

# Core Settings
HISTSIZE=1000000
SAVEHIST=1000000
setopt EXTENDED_HISTORY
setopt INC_APPEND_HISTORY
setopt SHARE_HISTORY
setopt HIST_EXPIRE_DUPS_FIRST
setopt HIST_IGNORE_DUPS
setopt HIST_FIND_NO_DUPS
setopt HIST_SAVE_NO_DUPS

# Completion System
autoload -Uz compinit
compinit
zstyle ':completion:*' menu select
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path ~/.zsh/cache

# Load plugins first
plugins=(git git-flow zsh-autosuggestions zsh-syntax-highlighting)
source $ZSH/oh-my-zsh.sh

# FZF Configuration (after plugins)
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh

# Enhanced FZF options
export FZF_DEFAULT_OPTS="--height 40% --layout=reverse --border --preview 'bat --style=numbers --color=always --line-range :500 {}'"
export FZF_DEFAULT_COMMAND='fd --type f --hidden --follow --exclude .git'
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
export FZF_CTRL_R_OPTS="--preview 'echo {}' --preview-window down:3:hidden:wrap --bind '?:toggle-preview'"

# Initialize fzf widgets
autoload -U fzf-cd-widget
zle -N fzf-cd-widget
bindkey '^[c' fzf-cd-widget

autoload -U fzf-file-widget
zle -N fzf-file-widget
bindkey '^T' fzf-file-widget

autoload -U fzf-history-widget
zle -N fzf-history-widget
bindkey '^R' fzf-history-widget

# Modern ls with exa
alias ls='eza --icons --group-directories-first'
alias ll='eza -l --icons --group-directories-first --git'
alias la='eza -la --icons --group-directories-first --git'
alias lt='eza --tree --icons --level=2'
alias l.='eza -d .* --icons'

🔌 Must-Have Plugins

  1. zsh-autosuggestions

    • Command suggestions based on history
    • Inline completion
    • Custom suggestion strategy
    • Fish-like experience
  2. zsh-syntax-highlighting

    • Command syntax highlighting
    • Error indication
    • Bracket matching
    • Path validation
    • Custom patterns and colors
  3. fzf

    • Fuzzy file finding
    • History search
    • Directory navigation
    • Command completion
    • Git integration
  4. git

    • Comprehensive Git aliases
    • Branch information
    • Status indicators
    • Common operations shortcuts

🎨 Theme Customization

  1. Framework Support

    • Oh My Zsh
    • Prezto
    • Antigen
    • Zinit
  2. Popular Themes

    • Powerlevel10k
    • Agnoster
    • Bureau
    • Robbyrussell
  3. Custom Elements

    • Git status
    • Python virtualenv
    • Node.js version
    • Exit status

💡 Pro Tips

  1. Performance Optimization

    # Lazy loading for slow commands
    nvm() {
    unset -f nvm
    source ~/.nvm/nvm.sh
    nvm "$@"
    }
  2. Custom Functions

    # Enhanced directory jumping with fzf
    j() {
    [ $# -gt 0 ] && _z "$*" && return
    cd "$(z -l | fzf --height 40% --reverse --inline-info | sed 's/^[0-9,.]* *//')"
    }
  3. Alias Management

    # Modern git shortcuts
    alias g='git status'
    alias gco='git checkout'
    alias gcp='git cherry-pick'

🚀 Getting Started

  1. Installation

    # Install ZSH
    sudo apt install zsh

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

    # Install essential tools
    sudo apt install fzf exa
  2. Plugin Installation

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

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

    # fzf completion and key bindings
    $(brew --prefix)/opt/fzf/install # On macOS
    /usr/share/doc/fzf/install # On Linux

🔧 Maintenance

  1. Regular Updates

    # Update Oh My Zsh
    omz update

    # Update plugins
    git -C \${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions pull
    git -C \${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting pull
  2. Cleanup

    # Clean command history
    echo "" > ~/.zsh_history

    # Rebuild completion cache
    rm ~/.zcompdump*
    compinit

🎯 Best Practices

  1. Configuration Organization

    • Keep main settings in ~/.zshrc
    • Use ~/.zshenv for environment variables
    • Create ~/.zsh/ for custom functions
    • Maintain separate plugin configs
  2. Performance Monitoring

    • Use zprof for profiling
    • Monitor startup time
    • Lazy load heavy integrations
    • Cache completion and other data