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 session
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
# Step 2: Install Oh My Zsh
echo "Installing Oh My Zsh..."
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
# Step 3: Install zsh plugins
echo "Installing ZSH plugins..."
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
git 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 configuration
echo "Configuring .zshrc..."
cat > ~/.zshrc << 'EOL'
# Path to your oh-my-zsh installation
export ZSH="$HOME/.oh-my-zsh"
# Set theme
ZSH_THEME="robbyrussell"
# Set plugins
plugins=(
git
macos
zsh-autosuggestions
zsh-syntax-highlighting
zsh-completions
)
# Load Oh My Zsh
source $ZSH/oh-my-zsh.sh
# Load auto-completion
autoload -Uz compinit
compinit
# Enhanced completion configuration
zstyle ':completion:*' menu select
zstyle ':completion:*' completer _expand _complete _ignored _approximate
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
zstyle ':completion:*' verbose yes
zstyle ':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-correction
setopt correct
# History configuration
HISTSIZE=10000
SAVEHIST=10000
HISTFILE=~/.zsh_history
setopt appendhistory
setopt sharehistory
setopt incappendhistory
setopt hist_ignore_all_dups
setopt hist_save_no_dups
setopt hist_ignore_space
setopt hist_verify
# Autosuggestion settings
ZSH_AUTOSUGGEST_STRATEGY=(history completion)
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=20
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=8"
bindkey '^ ' autosuggest-accept # Ctrl+Space to accept suggestion
# Useful macOS aliases
alias 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 completion
zstyle ':completion:*' special-dirs true
# Case-insensitive completion
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'
# Fix slow paste in zsh
pasteinit() {
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 pasteinit
zstyle :bracketed-paste-magic paste-finish pastefinish
# Set PATH
export PATH="/opt/homebrew/bin:$PATH"
EOL
# Step 5: Install additional helpful tools
echo "Installing additional tools..."
brew install exa # Modern replacement for ls
brew install bat # Better cat command
brew install fd # Better find command
brew install ripgrep # Better grep
# Step 6: Add additional tool configurations to .zshrc
cat >> ~/.zshrc << 'EOL'
# Modern command line tools
if 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 shortcuts
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
EOL
# Step 7: Source the new configuration
echo "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 usage
ls → exa
ll → exa -l
la → exa -la
bat (Enhanced cat)
# Syntax highlighting and line numbers
cat file.txt → bat file.txt
fd (Enhanced find)
# Faster, more intuitive file searching
find . -name "*.txt" → fd "*.txt"
ripgrep (Enhanced grep)
# Faster, smarter text searching
grep -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_THEME
ZSH_THEME="agnoster" # or any other theme
Add Custom Aliases
# Add to ~/.zshrc
alias dev="cd ~/Development"
alias ll="exa -la --git"
alias tree="exa --tree"
Install Additional Plugins
# Clone into the custom plugins directory
git clone https://github.com/plugin-name ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/plugin-name
# Add to plugins list in ~/.zshrc
plugins=(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 configuration
source ~/.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 Zsh
sh -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.