Zsh (Z Shell) 以強大的補全系統、豐富的提示符主題、命令糾錯與插件生態,成為開發者替代 Bash 的首選。本文以 Gentoo 為例 (適用通用),完整記錄從安裝、核心配置 (補全、糾錯、提示符) 到持久化生效的全過程。
emerge -av app-shells/zsh app-shells/zsh-completion
apt-get update && apt-get install -y zsh zsh-common
dnf install -y zsh zsh-completion # 或 yum
pacman -S zsh zsh-completions
zsh
# 出現 zsh-newuser-install 嚮導,輸入 q 跳過
# 選項:(q) Quit | (0) 建立空 .zshrc | (1) 進入主選單
autoload -U compinit
compinit
# 補全系統現在支援:命令/參數/路徑/變數/用戶/主機/進程/Package 等上下文感知補全
# 描述高亮
zstyle ':completion:*:descriptions' format '%U%B%d%b%u'
# 無匹配提示
zstyle ':completion:*:warnings' format '%BSorry, no matches for: %d%b'
# 選單選擇模式 (需 zsh/complist 模組)
zstyle ':completion:*' menu select
# 進程補全顯示 PID、命令、用戶
zstyle ':completion:*:processes' command 'ps -au$USER'
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'
setopt correctall
# 輸入 pythn -V 會提示:zsh: correct 'pythn' to 'python' [nyae]?
autoload -U promptinit
promptinit
prompt -l # 列出內建主題
prompt -p # 預覽所有主題
prompt gentoo # 啟用 gentoo 主題 (或 adam1, bart, elite2, fade, off, oliver, pws, redhat, suse, walters)
# 簡單範例
export PS1="[%*] %~ %# "
# 進階:顏色、Git 分支、返回碼、時間
autoload -U colors && colors
setopt prompt_subst
# Git 分支函數
git_prompt_info() {
local ref=$(git symbolic-ref --short HEAD 2>/dev/null)
[[ -n $ref ]] && echo " %F{yellow}($ref)%f"
}
# 最終 PS1
export PS1="%F{cyan}%n@%m%f %F{green}%~%f\$(git_prompt_info) %(?.%F{green}.%F{red})%#%f "
# 效果:user@host ~/project (main) # (綠色 # 表示上一命令成功,紅色表示失敗)
| 序列 | 顯示內容 |
|---|---|
%T | 系統時間 (HH:MM) |
%* | 系統時間 (HH:MM:SS) |
%D | 系統日期 (YY-MM-DD) |
%n | 用戶名 |
%m | 主機名 (第一個 . 之前) |
%M | 完整主機名 |
%~ | 相對 $HOME 的當前目錄 |
%d | 絕對當前目錄 |
%# | Root 顯示 #,普通用戶 % |
%? | 上一命令返回碼 |
%B...%b | 粗體 |
%U...%u | 下劃線 |
%F{color}...%f | 前景色 (red, green, blue, cyan, magenta, yellow, black, white, 0-255) |
~/.zshrccat >> ~/.zshrc << 'EOF'
# 補全系統
autoload -U compinit && compinit
zstyle ':completion:*:descriptions' format '%U%B%d%b%u'
zstyle ':completion:*:warnings' format '%BSorry, no matches for: %d%b'
zstyle ':completion:*' menu select
# 糾錯
setopt correctall
# 提示符
autoload -U promptinit && promptinit
prompt gentoo
# 或自定義 PS1 (見上文)
# 歷史優化
HISTSIZE=100000
SAVEHIST=100000
setopt hist_ignore_dups
setopt hist_ignore_space
setopt share_history
# 目錄堆疊
setopt auto_pushd
setopt pushd_ignore_dups
alias d='dirs -v'
# 實用別名
alias ll='ls -alhF --color=auto'
alias grep='grep --color=auto'
alias ..='cd ..'
alias ...='cd ../..'
EOF
chsh -s $(which zsh) # 當前用戶
# 或 root 執行:
usermod -s $(which zsh) username
# Oh My Zsh (最流行,社區最大)
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# Zinit (極速、冪等、支援 Turbo 模式)
bash -c "$(curl -fsSL https://git.io/zinit-install)"
# 推薦外掛
# zsh-users/zsh-autosuggestions # 灰色建議 (基於歷史)
# zsh-users/zsh-syntax-highlighting # 命令列語法高亮
# zsh-users/zsh-completions # 額外補全定義
# romkatv/powerlevel10k # 極速高顏值主題 (需安裝字體)
[[ ... ]] 語法完全相容for i in "${arr[@]}"export VAR=val 在 Zsh 中等同 typeset -x VAR=valsource 與 . 相容,但 Zsh 另有 zcompile 編譯腳本加速載入完成上述配置後,你將擁有具備智能補全、即時糾錯、資訊豐富提示符、強大歷史搜尋與外掛擴展的現代化 Shell 環境,大幅提升終端操作效率與體驗。
Ubuntu 關機卡住無法關機解決方案:GRUB 參數 acpi=force
Linux 開機啟動流程五階段深度解析:從 BIOS 到 systemd
Linux 網站備份新選擇:Bup (Git 為基礎) 增量備份、去重、還原與遠端同步完整教學
Linux 定時任務 Crontab 完整使用指南:從入門到實戰
Linux Shell 命令列操作大全:自動補全、歷史指令搜尋、編輯快捷鍵、作業控制、別名與函數定制
Linux 查看當前 Shell 的 7 種方法:$SHELL、$0、ps、/etc/passwd、echo $$ 等