923 words
5 minutes
Complete Automated ZSH Setup Script for macOS with Modern CLI Tools
Anubhav Gain
2025-03-02
Complete Automated ZSH Setup Script for macOS
This comprehensive script automates the setup of a fully configured ZSH environment with autosuggestions, autocompletion, and modern CLI tools on fresh macOS installations.
Features
- Homebrew Installation: Automatic package manager setup
- Oh My Zsh: Popular ZSH framework with themes and plugins
- ZSH Plugins: Autosuggestions, syntax highlighting, and completions
- Modern CLI Tools: Enhanced replacements for traditional Unix tools
- Optimized Configuration: Pre-configured .zshrc with performance optimizations
- Security Headers: Secure cookie and session management
- History Management: Advanced history configuration with deduplication
The Complete Setup Script
#!/bin/bash# Complete ZSH Setup Script for Fresh macOS# This script will set up a fully configured ZSH environment with autosuggestions and autocompletion
# Step 1: Install Homebrew (macOS package manager)echo "Installing Homebrew..."/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Add Homebrew to PATH for the current sessionecho 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofileeval "$(/opt/homebrew/bin/brew shellenv)"
# Step 2: Install Oh My Zshecho "Installing Oh My Zsh..."sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
# Step 3: Install zsh pluginsecho "Installing ZSH plugins..."git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestionsgit clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlightinggit clone https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-completions
# Step 4: Create a new .zshrc file with optimized configurationecho "Configuring .zshrc..."cat > ~/.zshrc << 'EOL'# Path to your oh-my-zsh installationexport ZSH="$HOME/.oh-my-zsh"
# Set themeZSH_THEME="robbyrussell"
# Set pluginsplugins=( git macos zsh-autosuggestions zsh-syntax-highlighting zsh-completions)
# Load Oh My Zshsource $ZSH/oh-my-zsh.sh
# Load auto-completionautoload -Uz compinitcompinit
# Enhanced completion configurationzstyle ':completion:*' menu selectzstyle ':completion:*' completer _expand _complete _ignored _approximatezstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"zstyle ':completion:*' verbose yeszstyle ':completion:*:descriptions' format '%B%d%b'zstyle ':completion:*:messages' format '%d'zstyle ':completion:*:warnings' format 'No matches for: %d'zstyle ':completion:*:corrections' format '%B%d (errors: %e)%b'zstyle ':completion:*' group-name ''
# Command auto-correctionsetopt correct
# History configurationHISTSIZE=10000SAVEHIST=10000HISTFILE=~/.zsh_historysetopt appendhistorysetopt sharehistorysetopt incappendhistorysetopt hist_ignore_all_dupssetopt hist_save_no_dupssetopt hist_ignore_spacesetopt hist_verify
# Autosuggestion settingsZSH_AUTOSUGGEST_STRATEGY=(history completion)ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=20ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=8"bindkey '^ ' autosuggest-accept # Ctrl+Space to accept suggestion
# Useful macOS aliasesalias showfiles="defaults write com.apple.finder AppleShowAllFiles YES && killall Finder"alias hidefiles="defaults write com.apple.finder AppleShowAllFiles NO && killall Finder"alias cleanup="find . -type f -name '*.DS_Store' -ls -delete"
# Enhanced path completionzstyle ':completion:*' special-dirs true
# Case-insensitive completionzstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'
# Fix slow paste in zshpasteinit() { OLD_SELF_INSERT=${${(s.:.)widgets[self-insert]}[2,3]} zle -N self-insert url-quote-magic}pastefinish() { zle -N self-insert $OLD_SELF_INSERT}zstyle :bracketed-paste-magic paste-init pasteinitzstyle :bracketed-paste-magic paste-finish pastefinish
# Set PATHexport PATH="/opt/homebrew/bin:$PATH"EOL
# Step 5: Install additional helpful toolsecho "Installing additional tools..."brew install exa # Modern replacement for lsbrew install bat # Better cat commandbrew install fd # Better find commandbrew install ripgrep # Better grep
# Step 6: Add additional tool configurations to .zshrccat >> ~/.zshrc << 'EOL'
# Modern command line toolsif command -v exa > /dev/null; then alias ls="exa" alias ll="exa -l" alias la="exa -la"fi
if command -v bat > /dev/null; then alias cat="bat"fi
# Useful shortcutsalias ..="cd .."alias ...="cd ../.."alias ....="cd ../../.."EOL
# Step 7: Source the new configurationecho "Sourcing new configuration..."source ~/.zshrc
echo "✅ ZSH setup complete!"echo "Your terminal has been configured with autosuggestions and autocompletion."echo "If you don't see the changes immediately, please restart your terminal."
What This Script Installs
1. Homebrew
- macOS package manager
- Enables installation of modern CLI tools
- Automatically added to PATH
2. Oh My Zsh Framework
- Popular ZSH configuration framework
- Provides themes and plugin management
- Uses the clean “robbyrussell” theme
3. Essential ZSH Plugins
zsh-autosuggestions
- Suggests commands as you type based on history
- Accept suggestions with
Ctrl+Space
- Configurable highlighting and strategy
zsh-syntax-highlighting
- Real-time syntax highlighting for commands
- Helps identify typos and invalid commands
- Color-coded command validation
zsh-completions
- Additional completion definitions
- Enhanced tab completion for many tools
- Better parameter and option completion
4. Modern CLI Tools
exa (Enhanced ls)
# Standard usagels → exall → exa -lla → exa -la
bat (Enhanced cat)
# Syntax highlighting and line numberscat file.txt → bat file.txt
fd (Enhanced find)
# Faster, more intuitive file searchingfind . -name "*.txt" → fd "*.txt"
ripgrep (Enhanced grep)
# Faster, smarter text searchinggrep -r "pattern" . → rg "pattern"
Key Configuration Features
1. History Management
- 10,000 command history with deduplication
- Shared history across all terminal sessions
- Ignore duplicates and commands starting with space
- History verification for recalled commands
2. Intelligent Completion
- Case-insensitive completion
- Menu selection for multiple matches
- Enhanced formatting with colors and descriptions
- Smart matching with error tolerance
3. Performance Optimizations
- Fast paste handling to prevent slowdowns
- Efficient plugin loading
- Optimized completion system
- Reduced startup time
4. macOS-Specific Features
- Show/hide hidden files commands
- DS_Store cleanup utility
- Homebrew PATH integration
- macOS plugin for system integration
Usage Instructions
- Save the script to a file (e.g.,
setup-zsh.sh
) - Make it executable:
chmod +x setup-zsh.sh
- Run the script:
./setup-zsh.sh
- Restart your terminal or run
source ~/.zshrc
Post-Installation Tips
Customize Your Theme
# Edit ~/.zshrc and change ZSH_THEMEZSH_THEME="agnoster" # or any other theme
Add Custom Aliases
# Add to ~/.zshrcalias dev="cd ~/Development"alias ll="exa -la --git"alias tree="exa --tree"
Install Additional Plugins
# Clone into the custom plugins directorygit clone https://github.com/plugin-name ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/plugin-name
# Add to plugins list in ~/.zshrcplugins=(git macos zsh-autosuggestions zsh-syntax-highlighting plugin-name)
Troubleshooting
Command Not Found
If you get “command not found” errors after installation:
# Reload your shell configurationsource ~/.zshrc# Or restart your terminal
Slow Terminal Startup
If your terminal starts slowly:
- Comment out unused plugins in ~/.zshrc
- Check for conflicting configurations
- Consider using a lighter theme
Plugin Issues
If plugins don’t work correctly:
# Reinstall Oh My Zshsh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"# Re-run the plugin installation steps
Benefits of This Setup
- Enhanced Productivity: Autosuggestions and completions speed up command entry
- Better User Experience: Syntax highlighting and modern tools improve terminal usability
- Consistent Environment: Standardized setup across multiple machines
- Easy Maintenance: Simple script for quick environment recreation
- Performance Optimized: Carefully tuned configuration for fast operation
This automated setup transforms your macOS terminal into a powerful, modern development environment with minimal manual configuration required.
Complete Automated ZSH Setup Script for macOS with Modern CLI Tools
https://mranv.pages.dev/posts/automated-macos-zsh-setup/