Zsh 是功能豐富的 Shell,提供強大補全、拼寫修正、主題提示符。本文記錄在 Gentoo (通用於各發佈版) 安裝 zsh 與 zsh-completion、啟用補全系統、改進補全樣式、開啟指令糾錯、配置提示符 (內建主題或自訂 PS1)、歷史紀錄去重、自動 cd、擴展 Globbing,並說明寫入 /etc/zsh/zshrc 或 ~/.zshrc 持久化、以 chsh 或 usermod 設為預設 Shell 的完整流程。
emerge zsh zsh-completion # Gentoo
# Ubuntu/Debian: apt-get install zsh zsh-common
# CentOS/RHEL: yum install zsh
zsh
# 輸入 q 跳過初始配置嚚導
autoload -U compinit
compinit
zstyle ':completion:*:descriptions' format '%U%B%d%b%u'
zstyle ':completion:*:warnings' format '%BSorry, no matches for: %d%b'
setopt correctall
autoload -U promptinit
promptinit
prompt -l # 列出內建主題
prompt gentoo # 套用 gentoo 主題
自訂 PS1 範例:
export PS1="[%* - %D] %d %% "
# [08:44:23 - 06-02-18] /home/user %
常用轉義序列:%T 時:分、%* 時:分:秒、%D 年-月-日、%n 用戶名、%m 短主機名、%~ 相對家目錄路徑。
export HISTSIZE=2000
export HISTFILE="$HOME/.history"
export SAVEHIST=$HISTSIZE
setopt hist_ignore_all_dups # 忽略重複指令
setopt hist_ignore_space # 以空白開頭不記錄
setopt autocd # 輸入目錄名直接 cd
setopt extendedglob # 擴展檔名匹配 (如 ^*.(tar|gz))
系統級:/etc/zsh/zshrc;用戶級:~/.zshrc。範例:
#!/bin/zsh
autoload -U compinit; compinit
setopt correctall
autoload -U promptinit; promptinit
prompt gentoo
usermod -s /bin/zsh usernamechsh -s /bin/zsh username~/.bashrc 加入 exec zsh。Zsh 以豐富補全、智慧糾錯、彈性提示符、強大歷史管理,大幅提升終端機互動體驗。配置一次,長期受益。