🎨 终端美化完全指南:Oh My Zsh + Starship + fzf,让你的命令行好看又好用
文章目录
- 🎨 终端美化完全指南:Oh My Zsh + Starship + fzf,让你的命令行好看又好用
- 🗺️ 整体方案一览
- 🔤 第一步:安装 Nerd Font(最先装,最重要)
- macOS
- Ubuntu / Linux
- 设置终端字体
- 🐚 第二步:安装 Zsh 并设为默认
- 🎭 第三步:安装 Oh My Zsh
- 安装三个必备插件
- 修改 `~/.zshrc` 启用插件
- ⭐ 第四步:安装 Starship(最重要的美化)
- 在 `~/.zshrc` 末尾添加启用命令
- 配置 Starship(关键步骤)
- 🔍 第五步:安装 fzf(模糊搜索神器)
- fzf 进阶配置(加入 `~/.zshrc`)
- 实际使用演示
- 🎨 第六步:终端颜色方案(锦上添花)
- macOS iTerm2
- Linux GNOME Terminal / Alacritty
- ⚡ 完整 `~/.zshrc` 配置
- 🪟 Windows 用户:WSL2 + Windows Terminal
- 🎁 速查:安装完后的效果
- 📣 最后
写在前面:每次打开终端看到那个单调的
$提示符,是不是有点提不起劲?这篇手把手带你把终端变成这样——彩色目录、git 分支状态、conda 环境、执行时间……全部显示在提示符里,还有按Ctrl+R模糊搜索历史命令、Ctrl+T模糊搜索文件。30 分钟完成,效果直接拉满。
🗺️ 整体方案一览
我们要安装的三个核心工具:
Oh My Zsh → zsh 插件框架(语法高亮、命令补全、快捷键) Starship → 提示符美化(git/conda/python版本/执行时间) fzf → 模糊搜索(历史命令、文件、目录)加上两个必备基础:
- Zsh:比默认 bash 更强的 shell
- Nerd Font:显示图标必须用的字体(装完字体才能看到漂亮图标)
适用系统:macOS + Ubuntu/Debian(Windows 用 WSL2)
🔤 第一步:安装 Nerd Font(最先装,最重要)
Starship 的图标需要 Nerd Font 才能正常显示,否则你会看到一堆方块。
推荐字体:FiraCode Nerd Font(编程连字 + 图标 = 完美)
macOS
# 用 Homebrew 安装(推荐)brew tap homebrew/cask-fonts brewinstall--caskfont-fira-code-nerd-font# 或者手动下载:# https://www.nerdfonts.com/font-downloads# 下载 FiraCode.zip,解压后双击 .ttf 文件安装Ubuntu / Linux
# 创建字体目录mkdir-p~/.local/share/fonts# 下载 FiraCode Nerd Fontwgethttps://github.com/ryanoasis/nerd-fonts/releases/latest/download/FiraCode.zipunzipFiraCode.zip-d~/.local/share/fonts/FiraCode# 刷新字体缓存fc-cache-fv# 验证安装fc-list|grep-i"FiraCode"设置终端字体
装完字体后,必须在终端设置里改字体,否则还是看不到图标:
- macOS iTerm2:Preferences → Profiles → Text → Font → 选
FiraCode Nerd Font - macOS Terminal:偏好设置 → 描述文件 → 字体
- Ubuntu GNOME Terminal:编辑 → 首选项 → 文本 → 自定义字体
- VSCode 终端:Settings →
terminal.integrated.fontFamily→'FiraCode Nerd Font'
🐚 第二步:安装 Zsh 并设为默认
# macOS(新版 macOS 已默认是 zsh,跳过安装)brewinstallzsh# Ubuntu / Debiansudoaptupdate&&sudoaptinstall-yzsh# 设为默认 shellchsh-s$(whichzsh)# 重新打开终端,验证echo$SHELL# 输出:/bin/zsh 或 /usr/bin/zsh ✅🎭 第三步:安装 Oh My Zsh
Oh My Zsh 是 zsh 的插件框架,内置 300+ 插件和 150+ 主题,我们用它管理插件(主题用 Starship 代替)。
# 方法1:官方安装(需要访问 GitHub)sh-c"$(curl-fsSLhttps://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"# 方法2:国内镜像(推荐)sh-c"$(curl-fsSLhttps://gitee.com/shmhlsy/oh-my-zsh-install.sh/raw/master/install.sh)"# 安装完成后 ~/.zshrc 会自动创建安装三个必备插件
# 1. zsh-autosuggestions:根据历史命令自动补全(灰色预览)gitclone https://github.com/zsh-users/zsh-autosuggestions\${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions# 国内镜像版:gitclone https://gitee.com/yantaozhao/zsh-autosuggestions.git\${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions# 2. zsh-syntax-highlighting:命令语法高亮(正确命令绿色,错误红色)gitclone https://github.com/zsh-users/zsh-syntax-highlighting\${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting# 国内镜像版:gitclone https://gitee.com/yantaozhao/zsh-syntax-highlighting.git\${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting# 3. zsh-z:快速跳转目录(比 cd 智能得多)gitclone https://github.com/agkozak/zsh-z\${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-z修改~/.zshrc启用插件
# 用你喜欢的编辑器打开nano~/.zshrc# 或 vim ~/.zshrc / code ~/.zshrc# 找到 plugins= 那一行,改成:plugins=(git# git 命令补全和别名zsh-autosuggestions# 历史命令自动补全zsh-syntax-highlighting# 语法高亮(放最后)zsh-z# 快速目录跳转conda-zsh-completion# conda 命令补全(做 AI 必装)docker# docker 命令补全python# python 相关别名)⭐ 第四步:安装 Starship(最重要的美化)
Starship 是用 Rust 写的超快提示符,支持所有 shell,一套配置到处用。
# macOSbrewinstallstarship# Linux(通用安装脚本)curl-sShttps://starship.rs/install.sh|sh# 或者用包管理器(Ubuntu)# snap install starship # 不太推荐,版本可能旧# 验证安装starship--version# starship 1.x.x在~/.zshrc末尾添加启用命令
echo'eval "$(starship init zsh)"'>>~/.zshrc配置 Starship(关键步骤)
创建配置文件:
mkdir-p~/.configtouch~/.config/starship.toml下面是我精心调整的配置,直接复制进去就能用:
# ~/.config/starship.toml # 提示符格式(每个模块的顺序) format = """ [╭─](bold bright-black)\ $username\ $hostname\ $directory\ $git_branch\ $git_status\ $conda\ $python\ $nodejs\ $time\ $line_break\ [╰─](bold bright-black)$character""" # 目录显示 [directory] style = "bold cyan" truncation_length = 3 truncate_to_repo = true read_only = " 🔒" # Git 分支 [git_branch] symbol = " " style = "bold purple" format = "[$symbol$branch]($style) " # Git 状态 [git_status] format = '([\[$all_status$ahead_behind\]]($style) )' style = "bold yellow" conflicted = "🔀" ahead = "⇡${count}" behind = "⇣${count}" diverged = "⇕⇡${ahead_count}⇣${behind_count}" untracked = "?" stashed = "$" modified = "!" staged = "+" renamed = "»" deleted = "✘" # Conda 环境(AI 开发必配) [conda] symbol = "🐍 " style = "bold green" format = "[$symbol$environment]($style) " ignore_base = false # Python 版本 [python] symbol = "🐍 " style = "bold yellow" format = '[${symbol}${pyenv_prefix}(${version})(\($virtualenv\))]($style) ' # 命令执行时间(超过 2 秒才显示) [cmd_duration] min_time = 2_000 format = "⏱ [$duration]($style) " style = "bold yellow" # 时间(右对齐) [time] disabled = false format = "[$time]($style) " style = "dimmed white" time_format = "%H:%M" # 用户名(SSH 时显示) [username] show_always = false format = "[$user]($style)@" style_user = "bold green" style_root = "bold red" # 主机名(SSH 时显示) [hostname] ssh_only = true format = "[$hostname]($style) " style = "bold blue" # 提示符字符(成功绿色,失败红色) [character] success_symbol = "[❯](bold green)" error_symbol = "[❯](bold red)" vimcmd_symbol = "[❮](bold green)" # Node.js [nodejs] symbol = " " format = "[$symbol($version)]($style) " style = "bold green"生效配置:
source~/.zshrc🔍 第五步:安装 fzf(模糊搜索神器)
fzf 是这套配置里最提升效率的工具:
Ctrl+R:模糊搜索命令历史(按关键词找历史命令)Ctrl+T:模糊搜索文件(不用记路径)Alt+C:模糊搜索目录并跳转**+ Tab:任意命令后模糊补全
# macOSbrewinstallfzf$(brew--prefix)/opt/fzf/install# 安装 shell 集成(一路 y)# Ubuntusudoaptinstallfzf# 安装 shell 集成$(dpkg-Lfzf|grepinstall)--all# 或手动:# 通用方法(git)gitclone--depth1https://github.com/junegunn/fzf.git ~/.fzf ~/.fzf/install# 一路 yfzf 进阶配置(加入~/.zshrc)
# fzf 主题(Catppuccin 莫奈风)exportFZF_DEFAULT_OPTS=" --color=fg:#cdd6f4,fg+:#cdd6f4,bg:#1e1e2e,bg+:#313244 --color=hl:#f38ba8,hl+:#f38ba8,info:#cba6f7,marker:#f5c2e7 --color=prompt:#cba6f7,spinner:#f5c2e7,pointer:#f5c2e7,header:#f38ba8 --color=border:#313244,label:#cdd6f4,query:#d9e0ee --border='rounded' --border-label='' --preview-window='border-rounded' --prompt='❯ ' --marker='✓' --pointer='◆' --layout=reverse --height=50%"# 用 fd 替代 find(更快)# 需要先安装:brew install fd / apt install fd-findexportFZF_DEFAULT_COMMAND='fd --type f --hidden --follow --exclude .git'exportFZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"# Alt+C 目录跳转exportFZF_ALT_C_COMMAND='fd --type d --hidden --follow --exclude .git'实际使用演示
# Ctrl+R:搜索历史命令# 按 Ctrl+R,输入 "train" → 列出所有包含 train 的历史命令# 用方向键选择,Enter 执行# Ctrl+T:搜索文件cat**<Tab># cat 后按 **+Tab,模糊搜索文件vim**<Tab># vim 打开文件时模糊搜索python **<Tab># 快速找 .py 文件# cd 目录模糊搜索cd**<Tab># 模糊搜索所有子目录# kill 进程(实时预览)kill<Tab># 列出所有进程,模糊搜索后 Enter 发送信号🎨 第六步:终端颜色方案(锦上添花)
光有漂亮的提示符还不够,终端背景和颜色方案也要配套。
macOS iTerm2
推荐Catppuccin Mocha配色(当前最流行的终端配色):
# 下载 iTerm2 配色方案curl-Ohttps://raw.githubusercontent.com/catppuccin/iterm/main/colors/catppuccin-mocha.itermcolors# 双击 .itermcolors 文件安装# iTerm2 → Preferences → Profiles → Colors → Color Presets → 选 catppuccin-mochaLinux GNOME Terminal / Alacritty
# Catppuccin for GNOME Terminal(一键安装脚本)curl-Lhttps://raw.githubusercontent.com/catppuccin/gnome-terminal/v0.2.0/install.py|python3 -⚡ 完整~/.zshrc配置
把下面这份完整配置覆盖~/.zshrc(安装完上面所有工具后):
# ═══════════════════════════════════════════# Oh My Zsh 基础配置# ═══════════════════════════════════════════exportZSH="$HOME/.oh-my-zsh"ZSH_THEME=""# 不用 OMZ 主题,用 Starshipplugins=(gitzsh-autosuggestions zsh-syntax-highlighting zsh-zdockerpython)source$ZSH/oh-my-zsh.sh# ═══════════════════════════════════════════# 环境变量# ═══════════════════════════════════════════exportLANG=en_US.UTF-8exportEDITOR='vim'exportPATH="$HOME/.local/bin:$PATH"# ═══════════════════════════════════════════# 常用别名# ═══════════════════════════════════════════aliasll='ls -la'aliasla='ls -A'alias..='cd ..'alias...='cd ../..'aliasgrep='grep --color=auto'# Git 快捷键aliasgs='git status'aliasga='git add'aliasgc='git commit -m'aliasgp='git push'aliasgl='git log --oneline --graph --decorate'# AI 开发专用aliasnv='nvidia-smi'aliasnvw='watch -n 1 nvidia-smi'aliasjl='jupyter lab'aliasta='tmux attach'# ═══════════════════════════════════════════# fzf 配置# ═══════════════════════════════════════════[-f~/.fzf.zsh]&&source~/.fzf.zshexportFZF_DEFAULT_OPTS=" --color=fg:#cdd6f4,bg:#1e1e2e,hl:#f38ba8 --color=fg+:#cdd6f4,bg+:#313244,hl+:#f38ba8 --color=info:#cba6f7,prompt:#cba6f7,pointer:#f5c2e7 --border='rounded' --prompt='❯ ' --marker='✓' --layout=reverse --height=50%"# ═══════════════════════════════════════════# 历史配置# ═══════════════════════════════════════════HISTSIZE=50000SAVEHIST=50000setopt HIST_IGNORE_DUPS# 不保存重复命令setopt HIST_IGNORE_SPACE# 以空格开头的命令不记录(隐私保护)setopt SHARE_HISTORY# 多个终端共享历史# ═══════════════════════════════════════════# 自动补全增强# ═══════════════════════════════════════════autoload-Ucompinit&&compinit zstyle':completion:*'menuselectzstyle':completion:*'matcher-list'm:{a-z}={A-Za-z}'# 大小写不敏感补全# ═══════════════════════════════════════════# Starship(必须放最后)# ═══════════════════════════════════════════eval"$(starship initzsh)"生效:
source~/.zshrc🪟 Windows 用户:WSL2 + Windows Terminal
Windows 用户用 WSL2 同样可以实现相同效果:
# 1. 安装 Windows Terminal(微软商店搜索安装)# 2. 安装 FiraCode Nerd Font# 下载:https://www.nerdfonts.com/font-downloads# 3. Windows Terminal 设置 → 配置文件 → 外观 → 字体 → FiraCode Nerd Font# 4. 进入 WSL2,按照 Linux 步骤安装 Zsh/OMZ/Starship/fzfWindows Terminal 配色方案(settings.json):
{"schemes":[{"name":"Catppuccin Mocha","background":"#1E1E2E","foreground":"#CDD6F4","cursorColor":"#F5E0DC","selectionBackground":"#585B70","black":"#45475A","red":"#F38BA8","green":"#A6E3A1","yellow":"#F9E2AF","blue":"#89B4FA","purple":"#CBA6F7","cyan":"#89DCEB","white":"#BAC2DE","brightBlack":"#585B70","brightRed":"#F38BA8","brightGreen":"#A6E3A1","brightYellow":"#F9E2AF","brightBlue":"#89B4FA","brightPurple":"#CBA6F7","brightCyan":"#89DCEB","brightWhite":"#A6ADC8"}]}🎁 速查:安装完后的效果
提示符显示内容:
╭─ ~/projects/llm-finetune main !+ 🐍 llm-env ⏱ 2m 30s 23:59 ╰─ ❯| 元素 | 含义 |
|---|---|
~/projects/llm-finetune | 当前目录(颜色:青色) |
main | git 分支(颜色:紫色) |
!+ | git 状态:!有修改,+有暂存 |
🐍 llm-env | 当前 conda 环境(颜色:绿色) |
⏱ 2m 30s | 上条命令执行时间(超过 2 秒才显示) |
23:59 | 当前时间 |
❯ | 成功绿色,出错变红色 |
📣 最后
如果这篇帮你把终端变漂亮又好用了:
- 👍点赞让更多人看到这篇
- ⭐收藏换电脑 / 配新服务器时照着装
- 💬评论参与投票,或者晒出你的终端截图!
- 🔔关注持续更新开发环境配置,一个正在学 AI 的大学生 👨🎓
📚相关阅读:
- 《Linux 命令大全:AI 开发必知的 80 个命令》
- 《Python 环境管理终极指南:conda vs venv vs uv》
📖参考资料:
- Starship 官方文档(starship.rs)
- Oh My Zsh GitHub(ohmyzsh/ohmyzsh)
- fzf GitHub(junegunn/fzf)
- Catppuccin 配色方案(catppuccin.com)