1. 安装 Zsh

macOS

1
2
which zsh
brew install zsh

Ubuntu / Debian

1
2
sudo apt update
sudo apt install -y zsh

CentOS / RHEL / Fedora

1
2
3
sudo yum install -y zsh
# 或
sudo dnf install -y zsh

2. 设置 Zsh 为默认 Shell

1
2
3
4
5
6
# 当前会话切换
zsh
# 设为默认 shell
chsh -s "$(which zsh)"
# 验证
echo $SHELL

⚠️ 若 chsh 失败,请确认:

1
cat /etc/shells | grep zsh

3. 安装 Oh My Zsh

官方方式(推荐)

1
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

国内镜像(网络较慢时)

1
sh -c "$(curl -fsSL https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh)"

🔐 安全提示 以上命令会直接执行远程脚本。在受限或生产环境中,建议先下载脚本并审阅。

查看版本信息

1
2
3
4
 omz version
master (ec14da7)
 zsh --version
zsh 5.9 (x86_64-debian-linux-gnu)

4. 插件体系设计原则(重要)

4.1 插件加载方式说明

  • Oh My Zsh 自带插件加载机制
  • 本文 不混用 zinit / antigen
  • 插件通过 ~/.zshrcplugins=(...) 控制
image-20260118101557094

4.2 推荐插件分层

✅ 核心必选(强烈推荐)

1
2
3
4
5
git
sudo
history
colored-man-pages
extract

✅ 体验增强(推荐)

1
2
3
z
zsh-autosuggestions
zsh-syntax-highlighting

⚙️ 开发工具类(按需)

1
2
3
4
5
6
7
8
docker
docker-compose
kubectl
node
npm
python
pip
brew

⚠️ 不建议一次性全开,插件越多,启动越慢

5. 配置插件(~/.zshrc)

编辑文件:

1
2
3
nano ~/.zshrc
# 或
vim ~/.zshrc

5.1 推荐插件配置(稳定 & 性能可控)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
plugins=(
  git
  sudo
  history
  extract
  colored-man-pages
  z
  zsh-autosuggestions
  zsh-syntax-highlighting
)

注意:

  • zsh-syntax-highlighting 必须放在最后
  • zautojump 功能重叠,建议二选一

6. 安装额外插件

1
mkdir -p ~/.oh-my-zsh/custom/plugins

6.1 zsh-autosuggestions(命令提示)

1
2
git clone https://github.com/zsh-users/zsh-autosuggestions \
${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

6.2 zsh-syntax-highlighting(语法高亮)

1
2
git clone https://github.com/zsh-users/zsh-syntax-highlighting \
${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

6.3 autojump(可选,替代 z)

1
2
3
4
5
6
# macOS
brew install autojump
# Ubuntu / Debian
sudo apt install -y autojump
# CentOS / RHEL
sudo yum install -y autojump

7. 主题配置

7.1 默认主题(稳定)

1
ZSH_THEME="robbyrussell"

7.2 Powerlevel10k(推荐但可选)

1
2
3
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git \
${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
ZSH_THEME="powerlevel10k/powerlevel10k"

首次加载:

1
p10k configure

⚠️ 推荐安装 Nerd Font(如 MesloLGS NF),否则图标可能异常

7.3 NerdFont字体安装

macOS通过命令安装:

1
2
3
4
brew tap homebrew/cask-fonts
brew install --cask font-meslo-lg-nerd-font
# 验证是否安装成功
ls ~/Library/Fonts | grep Meslo
image-20260118104250444

macOS手动安装:

1
2
3
4
5
6
7
8
9
# 下载
curl -L -o Meslo.zip \
https://github.com/ryanoasis/nerd-fonts/releases/latest/download/Meslo.zip

# 解压
unzip Meslo.zip -d MesloFont

# 安装
cp MesloFont/*.ttf ~/Library/Fonts

Ubuntu 安装 Nerd Font(用户级安装,无需 sudo):

1
2
3
4
5
6
7
8
mkdir -p ~/.local/share/fonts
cd ~/.local/share/fonts

curl -LO \
https://github.com/ryanoasis/nerd-fonts/releases/latest/download/Meslo.zip

unzip Meslo.zip
fc-cache -fv
image-20260118104358502 image-20260118104451600

系统级安装(不推荐,需 sudo):

1
2
3
sudo mkdir -p /usr/share/fonts/nerd-fonts
sudo unzip Meslo.zip -d /usr/share/fonts/nerd-fonts
sudo fc-cache -fv

8. 常用 Zsh 配置(推荐)

基础命令:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 启用自动补全
autoload -U compinit && compinit

# 大小写不敏感补全
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'

# 历史命令配置
HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000
setopt appendhistory
setopt sharehistory
setopt incappendhistory

# ===== 补全 =====
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'

# ===== 行为 =====
unsetopt correct_all

# ===== 颜色 =====
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced

# ===== 别名 =====
alias ll='ls -la'
alias la='ls -A'
alias l='ls -CF'
alias updatezsh="omz update"
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."

# 禁用自动更正
unsetopt correct_all

# 启用彩色输出
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced

PROMPT提示符配置:

image-20260119092946758 image-20260119093056760

提示符展示效果:

Clipboard_Screenshot_1768786326

9. 应用配置

1
2
source ~/.zshrc
# 或重启终端

性能建议

  • 插件数量建议 ≤ 8
  • kubectl / docker / node 补全插件会明显拖慢启动
  • 启动慢可用:
1
zsh -xv

11. 故障排查

1
2
3
4
5
6
7
8
# 查看插件
echo $plugins[@]

# 重置配置
omz reset

# 更新
omz update

image-20260119122119014

12. 备份与恢复

1
2
cp ~/.zshrc ~/.zshrc.backup
cp -r ~/.oh-my-zsh ~/.oh-my-zsh.backup