Oh My Zsh终端美化-打造高颜值开发环境

一、为什么选择Zsh?

Zsh(Z Shell)是Linux/Unix系统下的一个强大的Shell,比Bash更强大、更智能。

1.1 Zsh特点

1
2
3
4
5
6
7
8
9
10
11
┌─────────────────────────────────────────────────────────────────┐
│ Zsh优势 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ 🔸 智能补全 → 命令、参数、文件路径都能补全 │
│ 🔸 插件丰富 → 语法高亮、自动建议、git提示等 │
│ 🔸 主题丰富 → 界面美观,提升心情 │
│ 🔸 共享历史 → 多终端共享命令历史 │
│ 🔸 快速跳转 → 快速跳转到常用目录 │
│ │
└─────────────────────────────────────────────────────────────────┘

1.2 Oh My Zsh是什么?

Oh My Zsh是一个用于管理Zsh配置的社区框架,内置了大量插件和主题,让终端变得好看又好用。


二、安装Zsh

2.1 Ubuntu/Debian

1
2
3
4
5
6
7
8
9
10
# 安装zsh
sudo apt update
sudo apt install -y zsh

# 设为默认Shell
chsh -s /bin/zsh

# 验证
echo $SHELL
# 应该显示: /bin/zsh 或 /usr/bin/zsh

2.2 macOS

1
2
3
4
5
6
# macOS默认已安装zsh
# 如果没有
brew install zsh

# 设为默认Shell
chsh -s /bin/zsh

2.3 验证安装

1
2
3
4
5
6
7
# 查看版本
zsh --version
# zsh 5.9

# 测试zsh
zsh
# 会显示首次配置菜单

三、安装Oh My Zsh

3.1 自动安装

1
2
3
4
5
# 通过curl安装
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# 通过wget安装
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

3.2 手动安装

1
2
3
4
5
6
7
8
# 克隆仓库
git clone https://github.com/ohmyzsh/ohmyzsh.git ~/.oh-my-zsh

# 创建配置
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc

# 重新加载配置
source ~/.zshrc

四、主题配置

4.1 切换主题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 编辑配置文件
vim ~/.zshrc

# 找到 ZSH_THEME
ZSH_THEME="robbyrussell"

# 可选主题
ZSH_THEME=" agnoster" # 最流行
ZSH_THEME="powerlevel10k" # 最炫酷
ZSH_THEME="starship" # 现代简约
ZSH_THEME="pure" # 极简主义

# 保存后生效
source ~/.zshrc

4.2 常用主题推荐

主题 特点
robbyrussell 默认主题,简洁
agnoster 最流行,信息丰富
powerlevel10k 颜值高,速度快
starship 现代跨平台
pure 极简风格
bullet-train 简洁大方

4.3 安装Powerlevel10k

1
2
3
4
5
6
7
8
9
# 克隆主题
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

# 配置
vim ~/.zshrc
ZSH_THEME="powerlevel10k/powerlevel10k"

# 初始化配置向导
p10k configure

4.4 安装Starship

1
2
3
4
5
6
7
8
9
10
11
12
# 安装starship
curl -sS https://starship.rs/install.sh | sh

# 配置
vim ~/.zshrc

# 在文件末尾添加
eval "$(starship init zsh)"

# 创建配置
mkdir -p ~/.config
vim ~/.config/starship.toml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# ~/.config/starship.toml
format = """
[character]
success_symbol = "[➜](bold green) "
error_symbol = "[✗](bold red) "

[python]
symbol = " "

[git_branch]
symbol = " "

[cmd_duration]
min_time = 500

五、插件配置

5.1 启用插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 编辑配置
vim ~/.zshrc

# 找到 plugins=()
plugins=(
git
docker
pip
python
vscode
zsh-autosuggestions
zsh-syntax-highlighting
z
)

# 保存生效
source ~/.zshrc

5.2 必备插件

5.2.1 zsh-autosuggestions(自动建议)

1
2
3
4
5
# 安装
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

# 启用(在plugins中添加)
plugins=(... zsh-autosuggestions)

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

1
2
3
4
5
# 安装
git clone https://github.com/zsh-users/zsh-syntax-highlighting ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

# 启用
plugins=(... zsh-syntax-highlighting)

5.2.3 z(快速跳转)

1
2
3
4
5
# 无需安装,已内置
# 使用 z + 目录名 快速跳转
z blog # 跳转到包含"blog"的最近目录
z -l # 列出历史目录
z -c # 将当前目录加入记录

5.3 Git插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 内置Git插件,提供大量git别名
plugins=(... git)

# 常用别名
gcl → git clone
ga → git add
gc → git commit
gp → git push
gl → git pull
gd → git diff
gst → git status
gb → git branch
gco → git checkout
gm → git merge
gr → git rebase

5.4 Docker插件

1
2
3
4
5
6
7
# 安装docker-compose自动补全
mkdir -p ~/.zsh/completions
curl -L https://raw.githubusercontent.com/docker/cli/master/contrib/completion/zsh/_docker > ~/.zsh/completions/_docker
curl -L https://raw.githubusercontent.com/docker/compose/master/contrib/completion/zsh/_docker-compose > ~/.zsh/completions/_docker-compose

# 启用
plugins=(... docker docker-compose)

5.5 更多插件

插件 说明
zsh-history-substring-search 历史命令搜索
zsh-completions 额外补全
fzf 模糊搜索
bat cat增强版
exa ls增强版
fd find增强版

六、实用配置

6.1 基础配置

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
# ~/.zshrc

# ======= 基础设置 =======
# 启用颜色
export CLICOLOR=1

# 历史记录
HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000
setopt SHARE_HISTORY
setopt HIST_IGNORE_DUPS

# 自动纠正
setopt CORRECT
setopt CORRECT_ALL

# 智能引号
setopt AUTO_CD
setopt AUTO_PUSHD
setopt PUSHD_IGNORE_DUPS

# 补全
autoload -Uz compinit
compinit

6.2 别名配置

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
# ~/.zshrc 添加

# ======= 别名 =======
# 常用命令
alias ll='ls -lah'
alias la='ls -A'
alias l='ls -CF'

# git
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git pull'
alias gd='git diff'
alias gco='git checkout'

# docker
alias dps='docker ps'
alias dpsa='docker ps -a'
alias di='docker images'
alias dex='docker exec -it'

# python
alias py='python3'
alias pip='pip3'

# 网络
alias ip='curl -s https://ipinfo.io'

# 清屏
alias c='clear'

6.3 路径显示配置

1
2
3
4
5
6
7
8
9
10
11
12
13
# 显示完整路径
# 在主题中修改 PS1

# 或使用插件
# 在 .zshrc 中添加
DEFAULT_USER="$USER"

# 隐藏用户名(如果和当前用户一致)
prompt_context() {
if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
prompt_segment black default "%(!.%root.%n@)%m"
fi
}

6.4 启动画面

1
2
3
4
5
6
7
# 禁用启动时的"Today..."提示
# 在 ~/.zshrc 中添加
DISABLE_AUTO_UPDATE="true"
DISABLE_UPDATE_PROMPT="true"

# 禁用自动升级
DISABLE_AUTO_UPDATE="true"

七、工具推荐

7.1 exa(ls替代)

1
2
3
4
5
6
7
8
9
10
11
12
# 安装
# Ubuntu
sudo apt install exa

# macOS
brew install exa

# 配置别名
alias ls='exa --icons'
alias ll='exa -l --icons --group-directories-first'
alias la='exa -la --icons --group-directories-first'
alias lt='exa --tree --level=2'

7.2 bat(cat替代)

1
2
3
4
5
6
7
8
9
# 安装
# Ubuntu
sudo apt install bat

# macOS
brew install bat

# 配置别名
alias cat='bat'

7.3 fzf(模糊搜索)

1
2
3
4
5
6
7
# 安装
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install

# 使用
Ctrl+r # 搜索历史命令
Ctrl+t # 搜索文件

7.4 tldr(简化man)

1
2
3
4
5
6
7
8
9
# 安装
# Ubuntu
sudo apt install tldr

# macOS
brew install tldr

# 使用
tldr tar

八、实战配置

8.1 完整配置示例

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
40
41
42
43
44
45
46
47
48
49
50
51
52
# ======= ~/.zshrc 完整配置 =======

# 主题
ZSH_THEME="robbyrussell"

# 插件
plugins=(
git
docker
pip
python
zsh-autosuggestions
zsh-syntax-highlighting
z
)

# 自动更新
DISABLE_AUTO_UPDATE="true"

# 别名
alias ll='ls -lah'
alias la='ls -A'
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias dps='docker ps'

# 路径
export PATH="$HOME/bin:$PATH"

# 历史记录
HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000
setopt SHARE_HISTORY
setopt HIST_IGNORE_DUPS

# 补全
autoload -Uz compinit
compinit

# 颜色
export LSCOLORS=ExFxBxDxCxegedabagacad
export LS_COLORS='di=34:ln=35:so=32:pi=33:ex=31:bd=34;46:cd=34;43:su=30;41:sg=30:tw=30;42:ow=30;43'

# 中文支持
export LANG=zh_CN.UTF-8

# 编辑器
export EDITOR=vim
export VISUAL=vim

8.2 Powerlevel10k配置

1
2
3
4
5
6
7
8
9
10
11
12
13
# 安装后运行
p10k configure

# 或手动配置
# 在 ~/.zshrc 添加
ZSH_THEME="powerlevel10k/powerlevel10k"

# 创建配置
vim ~/.p10k.zsh

# 常用配置
typeset -g POWERLEVEL9K_INSTANT_PROMPT=quiet
typeset -g POWERLEVEL9K_DISABLE_HOT_RELOAD=true

九、常见问题

9.1 字体问题

1
2
3
4
5
6
7
8
9
# 安装Powerline字体
# Ubuntu
sudo apt install fonts-powerline

# macOS
brew install homebrew/cask-fonts/font-powerline

# 或使用Nerd Fonts
# https://www.nerdfonts.com/

9.2 加载慢

1
2
3
4
5
6
7
8
9
10
11
12
# 诊断
# 在 .zshrc 开头添加
zmodload zsh/zprof

# 测试
source ~/.zshrc
zprof

# 优化
# 1. 禁用不需要的插件
# 2. 延迟加载插件
# 3. 使用更轻量的主题

9.3 补全不工作

1
2
3
4
5
6
7
# 重新初始化
autoload -Uz compinit
compinit

# 清理缓存
rm -f ~/.zcompdump*
compinit

十、快捷键汇总

10.1 移动

快捷键 说明
Ctrl+a 行首
Ctrl+e 行尾
Alt+b 后退一个单词
Alt+f 前进一个单词

10.2 编辑

快捷键 说明
Ctrl+u 清除整行
Ctrl+k 清除到行尾
Ctrl+w 删除前一个单词
Ctrl+y 粘贴

10.3 历史

快捷键 说明
Ctrl+r 搜索历史
Ctrl+p 上一条命令
Ctrl+n 下一条命令
Ctrl+g 退出搜索

参考资料


持续更新中…欢迎收藏!

#Zsh #OhMyZsh #终端 #美化 #效率 #工具


Oh My Zsh终端美化-打造高颜值开发环境
https://r0f2.my/post/24-ohmyzsh-customization/
作者
JA
发布于
2026年2月13日
许可协议