Created
November 1, 2025 23:48
-
-
Save nuschpl/b3d15328f1774ad9622005a0d0fbe5ab to your computer and use it in GitHub Desktop.
Faster Oh-My-Zsh terminal open - optimization installer (macOS-safe, Bash 3.2 compatible)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Smarter Oh-My-Zsh optimization installer (macOS-safe, Bash 3.2 compatible) | |
| # - Backs up ~/.zshrc | |
| # - Removes previous optimization blocks (previous runs of this script) | |
| # - Detects existing 'source $ZSH/oh-my-zsh.sh' (various forms) | |
| # - Inserts PRE block BEFORE and POST block AFTER that exact line (no duplicate source) | |
| # - Adds async completion cache rebuild hooks for brew/pip/pipx/python3 -m pip | |
| # - Disables Docker Desktop and AWS CLI completion everytime terminal is opened | |
| # - removes duplicated ZSH completion from /usr/share/zsh/site-functions if Homebrew is present and has those under own path | |
| set -euo pipefail | |
| SINGLE_USER_WORKSTATION=true #way faster but insecure if you work within paths which can be written by others - _sudo completion can be installed | |
| ZSHRC="$HOME/.zshrc" | |
| BACKUP="${ZSHRC}.bak.$(date +%Y%m%d%H%M%S)" | |
| HOOK_DIR="$HOME/.config/zsh/hooks" | |
| HOOK_SCRIPT="$HOOK_DIR/rebuild_compdump.sh" | |
| # ---- Blocks with sentinels (use cat <<'EOF' … EOF) ---- | |
| PRE_BLOCK=$(cat <<'EOF' | |
| # >>> zsh-opt PRE: disable OMYZ compinit (managed) | |
| ZSH_DISABLE_COMPFIX=true | |
| skip_global_compinit=1 | |
| ZSH_DISABLE_COMPINIT=true | |
| # <<< zsh-opt PRE | |
| EOF | |
| ) | |
| POST_BLOCK=$(cat <<'EOF' | |
| # >>> zsh-opt POST: compinit + hooks (managed) | |
| if [[ "$SINGLE_USER_WORKSTATION" == true ]]; then | |
| # --- Fast, single-user setup (skip compaudit) --- | |
| ZSH_DISABLE_COMPFIX=true | |
| skip_global_compinit=1 | |
| ZSH_DISABLE_COMPINIT=true | |
| # Deduplicate and trim fpath intelligently | |
| if [[ -d /opt/homebrew/share/zsh/site-functions ]]; then | |
| # Drop system site-functions when Homebrew provides its own | |
| fpath=(${(@u)fpath}) | |
| fpath=(${fpath:#/usr/share/zsh/site-functions}) | |
| else | |
| fpath=(${(@u)fpath}) | |
| fi | |
| zstyle ':completion:*' use-cache on | |
| zstyle ':completion:*' cache-path ~/.zcompcache | |
| zstyle ':completion:*' rehash false | |
| ZCOMPDUMP="/tmp/zcompdump-\${USER}-\${HOST}.zwc" | |
| autoload -Uz compinit | |
| if [[ -f \$ZCOMPDUMP && ! ~/.zcompdump -nt \$ZCOMPDUMP ]]; then | |
| compinit -C -d "\$ZCOMPDUMP" | |
| else | |
| compinit -d "\$ZCOMPDUMP" | |
| zrecompile -p ~/.zcompdump >/dev/null 2>&1 | |
| fi | |
| else | |
| # --- Multi-user setup with compaudit enabled --- | |
| autoload -Uz compinit compaudit | |
| insecure=($(compaudit 2>/dev/null)) | |
| if [[ -n "$insecure" ]]; then | |
| echo "⚠️ Insecure completion directories detected:" | |
| printf ' %s\n' $insecure | |
| echo " (You can fix with: chmod -R go-w <path>)" | |
| fi | |
| compinit | |
| fi | |
| # helper to rebuild completions in background | |
| _rebuild_completions_async() { ~/.config/zsh/hooks/rebuild_compdump.sh & } | |
| # wrap tools to trigger async rebuilds after installs | |
| function brew() { | |
| command brew "$@" || return | |
| if [[ "$1" == "install" || "$1" == "upgrade" ]]; then _rebuild_completions_async; fi | |
| } | |
| function pip() { | |
| command pip "$@" || return | |
| if [[ "$1" == "install" || "$1" == install* ]]; then _rebuild_completions_async; fi | |
| } | |
| function pipx() { | |
| command pipx "$@" || return | |
| if [[ "$1" == "install" || "$1" == "inject" ]]; then _rebuild_completions_async; fi | |
| } | |
| function python3() { | |
| if [[ "$1" == "-m" && "$2" == "pip" && "$3" == "install" ]]; then | |
| command python3 "$@" || return | |
| _rebuild_completions_async | |
| else | |
| command python3 "$@" | |
| fi | |
| } | |
| # <<< zsh-opt POST | |
| EOF | |
| ) | |
| HOOK_BODY=$(cat <<'EOF' | |
| #!/bin/zsh | |
| # unified non-blocking rebuild of Zsh completions cache | |
| ( | |
| autoload -Uz compinit | |
| compinit | |
| if [[ ! -f ~/.zcompdump.zwc || ~/.zcompdump -nt ~/.zcompdump.zwc ]]; then | |
| zrecompile -p ~/.zcompdump >/dev/null 2>&1 | |
| fi | |
| ) >/dev/null 2>&1 & | |
| EOF | |
| ) | |
| echo "🔧 Backing up $ZSHRC → $BACKUP" | |
| cp -v "$ZSHRC" "$BACKUP" || true | |
| echo "🧹 Cleaning previous optimization blocks..." | |
| sed -i '' '/# >>> zsh-opt PRE: /,/# <<< zsh-opt PRE/d' "$ZSHRC" 2>/dev/null || true | |
| sed -i '' '/# >>> zsh-opt POST: /,/# <<< zsh-opt POST/d' "$ZSHRC" 2>/dev/null || true | |
| sed -i '' '/_rebuild_completions_async()/,/^}/d' "$ZSHRC" 2>/dev/null || true | |
| sed -i '' '/function brew()/,/^}/d' "$ZSHRC" 2>/dev/null || true | |
| sed -i '' '/function pip()/,/^}/d' "$ZSHRC" 2>/dev/null || true | |
| sed -i '' '/function pipx()/,/^}/d' "$ZSHRC" 2>/dev/null || true | |
| sed -i '' '/function python3()/,/^}/d' "$ZSHRC" 2>/dev/null || true | |
| echo "🧹Disabling Docker Desktop and AWS CLI shell completion everytime terminal is opened..." | |
| sed -i '' '/autoload -Uz compinit && compinit && complete -C .*aws_completer/d' "$ZSHRC" 2>/dev/null || true | |
| sed -i '' '/autoload -Uz compinit/{n;/compinit/;d;}' "$ZSHRC" 2>/dev/null || true | |
| echo "🧠 Ensuring hook script..." | |
| mkdir -p "$HOOK_DIR" | |
| printf "%s\n" "$HOOK_BODY" > "$HOOK_SCRIPT" | |
| chmod +x "$HOOK_SCRIPT" | |
| echo "🔎 Locating existing 'oh-my-zsh' source line…" | |
| # Detect 'source $ZSH/oh-my-zsh.sh' (macOS/BSD-safe, simple regex) | |
| SOURCE_LINE=$(grep -nE '^[[:space:]]*(source|\.)[[:space:]]*\$ZSH/oh-my-zsh\.sh[[:space:]]*$' "$ZSHRC" | head -n1 | cut -d: -f1) | |
| if [[ -n "$SOURCE_LINE" ]]; then | |
| echo "✅ Found 'source \$ZSH/oh-my-zsh.sh' on line $SOURCE_LINE — inserting blocks…" | |
| TMP="$(mktemp)" | |
| PRE_TMP="$(mktemp)" | |
| POST_TMP="$(mktemp)" | |
| printf "%s\n" "$PRE_BLOCK" > "$PRE_TMP" | |
| printf "%s\n" "$POST_BLOCK" > "$POST_TMP" | |
| awk -v prefile="$PRE_TMP" -v postfile="$POST_TMP" -v ln="$SOURCE_LINE" ' | |
| NR==ln { | |
| while ((getline line < prefile) > 0) print line | |
| print $0 | |
| while ((getline line < postfile) > 0) print line | |
| close(prefile); close(postfile) | |
| next | |
| } | |
| {print $0} | |
| ' "$ZSHRC" > "$TMP" | |
| mv "$TMP" "$ZSHRC" | |
| rm -f "$PRE_TMP" "$POST_TMP" | |
| else | |
| echo "⚠️ No explicit 'source $ZSH/oh-my-zsh.sh' line found." | |
| echo "🔍 Showing all lines mentioning oh-my-zsh for manual inspection:" | |
| grep -n -i "oh-my-zsh" "$ZSHRC" || echo "(none found)" | |
| echo "👀 Please verify which line actually sources Oh-My-Zsh." | |
| echo "Appending optimization blocks to the end for now." | |
| { | |
| printf "\n%s\n\n" "$PRE_BLOCK" | |
| echo "# NOTE: Ensure Oh-My-Zsh is sourced above these lines." | |
| printf "%s\n" "$POST_BLOCK" | |
| } >> "$ZSHRC" | |
| fi | |
| echo | |
| echo "✅ Done. Reload shell: exec zsh -l" | |
| echo "Expected:" | |
| echo " • Faster startup (single, cached compinit)" | |
| echo " • No repeated 're-compiling ~/.zcompdump.zwc' message" | |
| echo " • Completions auto-refresh after brew/pip/pipx/python3 installs" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment