Zsh
Zsh 是 macOS 的預設 shell,具備高度自訂性且與 Bash 相容。
使用時機
- 高效率互動式 Shell:互動式自動補全、模糊導航、歷史搜尋與分支指示器。
- 現代 CLI 外掛生態系:透過
zsh-autosuggestions、zsh-syntax-highlighting及 Starship 提示字元加速 shell 工作流程。 - 強健的 Shell 腳本:撰寫 POSIX 相容及 Zsh 擴充的自動化腳本,運用陣列操作與 globbing 功能。
- 企業環境管理:在 macOS 與 Linux 伺服器上管理 SDK 版本、PATH 匯出與別名。
快速開始
1. 最小化 ~/.zshrc 設定
# 歷史記錄設定
HISTFILE="$HOME/.zsh_history"
HISTSIZE=50000
SAVEHIST=50000
setopt HIST_IGNORE_DUPS
setopt SHARE_HISTORY
# 按鍵綁定 (emacs 模式)
bindkey -e
# 自動補全
autoload -Uz compinit && compinit
# 別名
alias ll="ls -lah"
alias g="git"
alias gs="git status -sb"
2. 基準測試啟動時間
# 評估 Zsh 啟動時間
zmodload zsh/zprof
time zsh -i -c exit
核心概念
高速現代 Zshrc (~/.zshrc)
設定低於 20 毫秒的 shell 啟動、智慧補全與歷史記錄:
# ~/.zshrc
# 歷史記錄設定
HISTFILE="$HOME/.zsh_history"
HISTSIZE=50000
SAVEHIST=50000
setopt EXTENDED_HISTORY
setopt HIST_EXPIRE_DUPS_FIRST
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_SPACE
setopt HIST_VERIFY
setopt SHARE_HISTORY
# 目錄導航選項
setopt AUTO_CD
setopt AUTO_PUSHD
setopt PUSHD_IGNORE_DUPS
setopt PUSHD_SILENT
# 快速補全系統 (含快取)
autoload -Uz compinit
typeset -i updated_at=$(date +'%j' -r ~/.zcompdump 2>/dev/null || stat -f '%Sm' -t '%j' ~/.zcompdump 2>/dev/null)
if [ $(date +'%j') != $updated_at ]; then
compinit -i
else
compinit -C -i
fi
# 現代按鍵綁定:歷史搜尋
bindkey -e
bindkey '^[[A' history-search-backward
bindkey '^[[B' history-search-forward
# 初始化 Starship 提示字元 (若已安裝)
if command -v starship &> /dev/null; then
eval "$(starship init zsh)"
fi
無需龐大框架的輕量外掛載入
直接載入自動建議與語法高亮:
# 將外掛複製到 ~/.zsh/plugins/
# git clone https://github.com/zsh-users/zsh-autosuggestions ~/.zsh/plugins/zsh-autosuggestions
# git clone https://github.com/zsh-users/zsh-syntax-highlighting ~/.zsh/plugins/zsh-syntax-highlighting
source ~/.zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
source ~/.zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
# 設定建議文字顏色
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'
進階 Zsh Globbing 與參數展開
利用遞迴 globbing 與修飾符:
# 列出過去 24 小時內修改的所有 TypeScript 檔案
ls -lh **/*.ts(m-1)
# 遞迴刪除所有空目錄
rmdir -p **/*(/^F) 2>/dev/null
# 只選取 bin/ 目錄中的可執行檔
chmod +x bin/*(*)
常見模式
使用 Zinit 高速載入外掛
問題:Oh My Zsh 因大量急迫載入導致啟動時間長達數百毫秒。
解決方案:使用 Zinit 的 Turbo 模式(延遲載入)。
# 若缺少 Zinit,先進行安裝
source ~/.local/share/zinit/zinit.git/zinit.zsh
# 非同步 Turbo 載入語法高亮與自動建議
zinit wait lucid for \
atinit"ZINIT[COMPINIT_OPTS]=-C; zicompinit; zicdreplay" \
zdharma-continuum/fast-syntax-highlighting \
atload"_zsh_autosuggest_start" \
zsh-users/zsh-autosuggestions \
blockf \
zsh-users/zsh-completions
強健的 Shell 腳本函式
問題:撰寫含選項解析與錯誤處理的可重複使用 shell 函式。
解決方案:使用標準的 Zsh 參數展開。
# mkcd:建立目錄並切換進去
mkcd() {
if [[ -z "$1" ]]; then
echo "用法: mkcd <目錄>" >&2
return 1
fi
mkdir -p "$1" && cd "$1"
}
最佳實踐 (2026)
- 請務必 將
~/.zcompdump編譯為位元組碼檔案 (compdump.zwc),以加速 shell 啟動時間。 - 請務必 設定
setopt SHARE_HISTORY以同步所有作用中終端機頁籤的指令歷史。 - 請務必 使用 Starship 或精簡的原生提示字元,避免使用肥大過時的提示主題。
- 請務必 使用
zmodload zsh/zprof在~/.zshrc的開頭和結尾進行啟動延遲分析。 - 請勿 使用肥大、笨重的框架設定(如單體式 Oh-My-Zsh 設定)並載入數十個不需要的外掛。
- 請勿 將純文字雲端憑證、API 權杖或機密資訊儲存在
~/.zshrc中;請使用私有的.env檔案或機密保管庫。 - 請勿 在每次互動式 shell 啟動時同步執行慢速的外部指令(如
nvm、brew doctor)。
排錯
| 錯誤 / 症狀 | 原因 | 解決方案 |
|---|---|---|
編輯 .zshrc 後出現 zsh: command not found: ... | .zshrc 語法錯誤或 $PATH 被覆寫 | 正確地附加路徑:export PATH="$HOME/bin:$PATH" 並執行 source ~/.zshrc。 |
zsh compinit: insecure directories, run compaudit | /usr/local/share/zsh 目錄權限不安全 | 執行 `compaudit |
| Zsh 啟動極慢 (> 500ms) | 同步執行 nvm 或載入大量外掛 | 對 nvm 使用延遲載入,或在 .zshrc 開頭使用 zmodload zsh/zprof 並在結尾使用 zprof 進行分析。 |