Skip to content

Complete Automated ZSH Setup Script for macOS with Modern CLI Tools

Published: at 01:47 AM

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

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

2. Oh My Zsh Framework

3. Essential ZSH Plugins

zsh-autosuggestions

zsh-syntax-highlighting

zsh-completions

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

2. Intelligent Completion

3. Performance Optimizations

4. macOS-Specific Features

Usage Instructions

  1. Save the script to a file (e.g., setup-zsh.sh)
  2. Make it executable: chmod +x setup-zsh.sh
  3. Run the script: ./setup-zsh.sh
  4. 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:

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

  1. Enhanced Productivity: Autosuggestions and completions speed up command entry
  2. Better User Experience: Syntax highlighting and modern tools improve terminal usability
  3. Consistent Environment: Standardized setup across multiple machines
  4. Easy Maintenance: Simple script for quick environment recreation
  5. 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.