使用 zsh + oh-my-zsh + powerlevel10k 打造美观的 Linux 终端


1. 环境准备

1.1 安装必备工具

1
sudo apt install wget git vim
  • wget:下载安装脚本
  • git:克隆仓库
  • vim:编辑配置文件(也可用其他编辑器)

1.2 安装 Hack Nerd Fonts 字体

主题中的图标需要 Nerd Fonts 字体支持才能正常显示。

1
2
3
4
5
6
# GitHub
git clone --depth 1 https://github.com/ryanoasis/nerd-fonts.git

cd nerd-fonts
chmod +x install.sh
./install.sh

安装完成后重启系统,并在终端首选项中将字体设置为 Hack Nerd Font


2. 安装 zsh

1
2
sudo apt install zsh
chsh -s /bin/zsh # 将默认 shell 切换为 zsh

切换后需重新登录(或重启)生效。


3. 安装 oh-my-zsh

1
2
# 官方脚本(需能访问 GitHub)
sh -c "$(wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"

解压到用户主目录后:

1
2
chmod +x install.sh
./install.sh

安装过程中按提示按回车、输入密码即可。


4. 安装 powerlevel10k 主题

克隆到 oh-my-zsh 的自定义主题目录:

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

修改 ~/.zshrc

1
vim ~/.zshrc
  • ZSH_THEME="robbyrussell" 改为:

    1
    ZSH_THEME="powerlevel10k/powerlevel10k"
  • 添加一行:

    1
    POWERLEVEL9K_MODE="awesome-patched"

启动配置向导

1
2
3
source ~/.zshrc     # 首次加载会自动进入向导
# 或随时手动启动:
p10k configure

5. 推荐插件

5.1 zsh-autosuggestions(命令自动补全建议)

根据历史命令显示灰色建议,按 →(右方向键)补全。

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

5.2 zsh-syntax-highlighting(命令语法高亮)

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

5.3 启用插件

编辑 ~/.zshrc,找到 plugins=(git)(约第 81 行),改为:

1
plugins=(git zsh-autosuggestions zsh-syntax-highlighting)

然后 source ~/.zshrc 使其生效。


6. powerlevel10k 配置向导参考选项

运行 p10k configure 后进入交互式向导。操作说明:按提示的字母/数字键选择;不可回退;按 r 可重新开始。

配置生成后保存在 ~/.p10k.zsh,可随时重新运行 p10k configure 调整。

现代化 CLI 工具安装指南


0. 总览速查表

工具替代apt 包名(Ubuntu 24.04)Homebrew实际命令
aria2cwget / curlaria2aria2aria2c
batcatbatbatbatcat ⚠️
btoptopbtopbtopbtop
dufdfdufdufduf
dive—(镜像分析)divedive
ezalsezaezaeza
fdfindfd-findfdfdfind ⚠️
fqhexdumpfqfqfq
fzf模糊查找fzffzffzf
gdudugdugdugdu
gpingpinggpinggpinggping
httpcurl / telnethttpiehttpiehttp / https
hyperfinetimehyperfinehyperfinehyperfine
rggrepripgrepripgreprg
tiggittigtigtig
zcdzoxidezoxidez(需 shell 初始化)⚠️

⚠️ : 包名与命令名不一致,或需要额外配置,详见各节。

Ubuntu 24.04 一键安装(除 dive 外):

1
2
3
sudo apt update
sudo apt install -y aria2 bat btop duf eza fd-find fzf gdu gping \
httpie hyperfine ripgrep tig zoxide fq

macOS 一键安装(全部 16 个):

1
2
brew install aria2 bat btop duf dive eza fd fq fzf gdu gping \
httpie hyperfine ripgrep tig zoxide

1. aria2c — 替代 wget / curl

轻量级多协议、多来源下载工具,支持 HTTP/HTTPS、FTP、SFTP、BitTorrent、Metalink。多连接下载可充分利用带宽。

1
2
3
4
5
# Debian / Ubuntu
sudo apt install aria2

# macOS
brew install aria2

用法示例:aria2c -x16 http://example.com/file.iso(16 线程下载)


2. bat — 替代 cat

Rust 编写的 cat 增强版:语法高亮、Git 集成、自动分页、显示不可打印字符。

1
2
3
4
5
# Debian / Ubuntu
sudo apt install bat

# macOS
brew install bat

⚠️ Debian 系命名冲突:包名为 bat,但二进制命令是 batcat(与其他软件包冲突)。解决方法——创建符号链接:

1
sudo ln -s /usr/bin/batcat /usr/local/bin/bat

3. btop — 替代 top

C++ 编写的终端资源监控 TUI:CPU、内存、磁盘、网络、进程实时可视化,支持树状进程、过滤、活动图表。

1
2
3
4
5
# Debian / Ubuntu
sudo apt install btop

# macOS
brew install btop

4. duf — 替代 df

Go 编写的磁盘使用查看工具,比 df 更快速友好,支持文本与图形化显示。

1
2
3
4
5
# Debian / Ubuntu
sudo apt install duf

# macOS
brew install duf

5. dive — Docker 镜像分析(无传统替代)

分析 Docker/OCI 镜像各层内容,发现缩小镜像体积的方法。

1
2
3
4
5
6
7
# Debian / Ubuntu (注意选择对应架构 amd64/arm64)
curl -sL -o /tmp/dive.deb \
https://github.com/wagoodman/dive/releases/download/v0.13.1/dive_0.13.1_linux_arm64.deb
sudo dpkg -i /tmp/dive.deb

# macOS
brew install dive

6. eza — 替代 ls

现代 ls 替代品:按颜色区分文件类型与元数据,支持符号链接、扩展属性和 Git 信息。

1
2
3
4
5
# Debian / Ubuntu
sudo apt install eza

# macOS
brew install eza

推荐别名(加入 ~/.bashrc~/.zshrc):

1
2
alias ls='eza'
alias ll='eza -lbGF --git'

7. fd — 替代 find

Rust 编写的 find 替代品:语法直观、彩色输出、大小写智能,默认遵守 .gitignore

1
2
3
4
5
# Debian / Ubuntu
sudo apt install fd-find

# macOS
brew install fd

⚠️ Debian 系命名冲突:包名 fd-find,二进制命令为 fdfind。解决方法:

1
sudo ln -s /usr/bin/fdfind /usr/local/bin/fd

8. fq — 替代 hexdump

类似 jq 但面向二进制数据:支持多种文件格式、树状解码、转换/切片/连接、交互式 REPL。

1
2
3
4
5
# Debian / Ubuntu
sudo apt install fq

# macOS
brew install fq

用法示例:fq . filefq d filefq 'some query' file


9. fzf — 通用模糊查找器

可用于文件、命令历史、进程、主机名、书签、Git 提交等任何列表;与 shell、vim/neovim、tmux 深度集成。

1
2
3
4
5
# Debian / Ubuntu
sudo apt install fzf

# macOS
brew install fzf

10. gdu — 替代 du

Go 编写的快速磁盘使用分析器,带 TUI。原页测试:同一目录 ncdu 需 7.4 秒,gdu 毫秒级完成。

1
2
3
4
5
# Debian / Ubuntu
sudo apt install gdu

# macOS
brew install gdu

11. gping — 替代 ping

图形化 ping:多主机延迟测试、命令执行时间测试、颜色自定义。

1
2
3
4
5
# Debian / Ubuntu
sudo apt install gping

# macOS
brew install gping

12. http(HTTPie)— 替代 curl / telnet

人性化 HTTP 客户端:http/https 命令发送请求,语法简单,输出格式化彩色显示,适合调试 API。

1
2
3
4
5
# Debian / Ubuntu
sudo apt install httpie

# macOS
brew install httpie

13. hyperfine — 替代 time

命令行基准测试:多次运行并统计分析、预热/缓存清理、离群值检测,可导出 CSV、JSON、Markdown、AsciiDoc。

1
2
3
4
5
# Debian / Ubuntu
sudo apt install hyperfine

# macOS
brew install hyperfine

14. rg(ripgrep)— 替代 grep

递归正则搜索,默认遵守 .gitignore,速度极快。

1
2
3
4
5
# Debian / Ubuntu
sudo apt install ripgrep

# macOS
brew install ripgrep

15. tig — git 的终端 UI

基于 ncurses 的 Git 仓库界面:浏览提交历史、查看差异、逐行暂存、搜索、blame、cherry-pick。

1
2
3
4
5
# Debian / Ubuntu
sudo apt install tig

# macOS
brew install tig

16. z(zoxide)— 替代 cd

智能目录跳转:按频率排序常用目录,减少输入。

1
2
3
4
5
# Debian / Ubuntu
sudo apt install zoxide

# macOS
brew install zoxide

⚠️ 必须做 shell 初始化z 是 shell 函数而非独立二进制,实测确认)。按你的 shell 将对应行加入配置文件:

1
2
3
4
5
# bash → ~/.bashrc
eval "$(zoxide init bash)"

# zsh → ~/.zshrc
eval "$(zoxide init zsh)"

初始化后 z 命令即可用(实测 z is a function 验证通过)。