devAlice
← Mac

定制 zsh — starship 提示符、历史记录、别名、插件

30 分钟将 macOS 默认 zsh 打造成高效 Shell。starship + 历史记录搜索 + 自动建议 + 语法高亮。

macOS 自 Catalina 起默认使用 zsh,但开箱即用的配置还差得远。全量安装 Oh My Zsh 会带来额外负担并拖慢启动速度。本指南只定制你真正需要的 — starship 提示符、历史记录搜索、自动建议、语法高亮、fzf 以及五个实用别名。

我认为 zsh 定制的正确策略不是安装一个大框架,而是按需叠加最小单元。以前 Oh My Zsh 一站式解决了所有问题;如今逐项添加 starship 加插件的方式,因为每个组件独立可控,启动时间和维护成本都更低。

适合已完成 Mac 初始配置、希望打磨 zsh 的开发者——包括通过 dotfiles 将 .zshrc 同步到其他机器。

TL;DR

  1. starship — 快速美观的提示符(比 Oh My Zsh 更轻量)
  2. zsh-autosuggestions — 基于历史记录的灰色建议(fish 风格)
  3. zsh-syntax-highlighting — 输入时为命令着色
  4. fzf Shell 集成 — 现代化 Ctrl+R 历史记录搜索
  5. 通过 dotfiles.zshrc 提交到 chezmoi,其他机器自动同步

前提条件

1. 为什么默认 zsh 不够用

  • 提示符无 git 信息 — 分支 / 脏状态不可见
  • 历史记录搜索薄弱Ctrl+R 存在,但没有模糊搜索
  • 自动补全有限 — Tab 可以用,但没有 fish 风格的灰色建议
  • 无语法高亮 — 拼写错误只有提交后才能发现

修复这些问题,每天都会有回报。


2. starship 提示符(5 分钟)

starship.rs — 用 Rust 编写的快速跨 Shell 提示符。比 Oh My Zsh 的 Powerlevel10k 更轻量,启动开销接近零。

2.1 安装

brew install starship

.zshrc 底部添加:

eval "$(starship init zsh)"

打开新终端——立即生效。

2.2 Nerd Font(用于图标)

要看到 starship 的 git 分支和语言图标,需要 Nerd Font。

brew install --cask font-jetbrains-mono-nerd-font

将终端字体改为 JetBrainsMono Nerd Font。(参见 WezTerm 指南。)

2.3 调整(可选)

~/.config/starship.toml

# 两行提示符 — 上方显示信息,下方接收输入
add_newline = true
format = """
$directory$git_branch$git_status$nodejs$python$rust$line_break$character"""
 
[character]
success_symbol = "[➜](bold green)"
error_symbol = "[✗](bold red)"
 
[directory]
truncation_length = 3
truncate_to_repo = true
 
[git_branch]
symbol = " "
style = "bold purple"
 
[git_status]
conflicted = "⚠️ "
ahead = "⇡${count} "
behind = "⇣${count} "
staged = "[+${count}](green) "
modified = "[!${count}](yellow) "
untracked = "[?${count}](red) "

默认配置已经很好 — 先不创建 starship.toml,只调整让你感到不舒服的地方。


3. 历史记录 — 修复糟糕的默认值

添加到 .zshrc

# 历史记录大小
HISTSIZE=50000              # 内存中保留的行数
SAVEHIST=100000             # 保存到文件的行数
HISTFILE="$HOME/.zsh_history"
 
# 去重 + 立即保存
setopt HIST_IGNORE_ALL_DUPS  # 删除重复项
setopt HIST_REDUCE_BLANKS    # 合并只有空格差异的条目
setopt HIST_VERIFY           # 不自动执行 `!42` 风格的展开
setopt SHARE_HISTORY         # 所有 zsh 实例共享历史记录
setopt INC_APPEND_HISTORY    # 立即追加到历史文件
setopt EXTENDED_HISTORY      # 时间戳 + 执行时长
 
# 上/下键 — 前缀匹配搜索
autoload -U up-line-or-beginning-search down-line-or-beginning-search
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search
bindkey "^[[A" up-line-or-beginning-search    # 上键
bindkey "^[[B" down-line-or-beginning-search  # 下键

效果:输入 git c 后按 ↑ — 只循环过去以 git c 开头的命令。


4. zsh-autosuggestions(3 分钟)

Fish 风格的灰色下一条命令建议。按 Tab 或 → 接受。

brew install zsh-autosuggestions

.zshrc 底部添加:

source $(brew --prefix)/share/zsh-autosuggestions/zsh-autosuggestions.zsh
 
# 灰色调(默认颜色太深)
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=#5f5f5f'
 
# 跳过很长命令的建议(性能考虑)
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=80

新终端 → 输入命令 → 看到灰色建议 = 成功。


5. zsh-syntax-highlighting(2 分钟)

输入时为命令着色 — 有效显示绿色,无效显示红色,选项显示黄色等。

brew install zsh-syntax-highlighting

.zshrc 底部添加——必须在 zsh-autosuggestions 之后

source $(brew --prefix)/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

顺序很重要

  1. autosuggestions
  2. syntax-highlighting(必须最后)

顺序反转,两者偶尔都会出问题。


6. fzf — 现代化 Ctrl+R 历史记录搜索

默认 Ctrl+R 逐行回溯。fzf 提供模糊搜索 + 预览。

brew install fzf
$(brew --prefix)/opt/fzf/install --all

安装脚本会在 .zshrc 中添加:

[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
  • Ctrl + R — 模糊历史记录搜索
  • Ctrl + T — 模糊选择当前目录中的文件(如 vim <Ctrl+T>
  • Alt + C — 模糊 cd

更多信息:Mac 初始配置 §3.2


7. 五个实用别名

.zshrc 或单独的 ~/.zsh_aliases(推荐):

# git
alias g='git'
alias gs='git status -s'
alias gd='git diff'
alias gco='git checkout'
alias gp='git push'
alias gl='git log --oneline --graph --decorate -20'
 
# 文件系统(eza — 现代 ls)
alias ls='eza --git --group-directories-first'
alias ll='eza -l --git --group-directories-first --icons'
alias la='eza -la --git --group-directories-first --icons'
alias tree='eza --tree --level=3'
 
# 目录导航
alias ..='cd ..'
alias ...='cd ../..'
 
# 重新加载
alias zrl='source ~/.zshrc'
 
# 编辑器(VS Code 或 Cursor)
alias c='code .'

单字母别名有冲突风险。g(git)和 c(code)在意图明确时没问题。


8. 目录跳转 — zoxide

不用输入 /Users/me/projects/myapp,只需 z myapp 一步跳转。自动学习你常访问的目录。

brew install zoxide

.zshrc

eval "$(zoxide init zsh --cmd cd)"  # 用 zoxide 替换 cd 本身

正常使用 cd,zoxide 自动学习。之后 cd myapp 通过部分名称跳转。


9. 完整 .zshrc 示例

整合以上所有内容的完整参考:

# ~/.zshrc
 
# ─────────────────────────────────────────────
# PATH
# ─────────────────────────────────────────────
export PATH="$HOME/.local/bin:$PATH"
 
# ─────────────────────────────────────────────
# 历史记录
# ─────────────────────────────────────────────
HISTSIZE=50000
SAVEHIST=100000
HISTFILE="$HOME/.zsh_history"
setopt HIST_IGNORE_ALL_DUPS HIST_REDUCE_BLANKS HIST_VERIFY
setopt SHARE_HISTORY INC_APPEND_HISTORY EXTENDED_HISTORY
 
# 上/下键 — 前缀搜索
autoload -U up-line-or-beginning-search down-line-or-beginning-search
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search
bindkey "^[[A" up-line-or-beginning-search
bindkey "^[[B" down-line-or-beginning-search
 
# ─────────────────────────────────────────────
# 别名
# ─────────────────────────────────────────────
alias g='git'
alias gs='git status -s'
alias gd='git diff'
alias gp='git push'
alias gl='git log --oneline --graph --decorate -20'
alias ls='eza --git --group-directories-first'
alias ll='eza -l --git --group-directories-first --icons'
alias la='eza -la --git --group-directories-first --icons'
alias ..='cd ..'
alias ...='cd ../..'
alias zrl='source ~/.zshrc'
alias c='code .'
 
# ─────────────────────────────────────────────
# 插件(顺序很重要)
# ─────────────────────────────────────────────
source $(brew --prefix)/share/zsh-autosuggestions/zsh-autosuggestions.zsh
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=#5f5f5f'
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=80
 
source $(brew --prefix)/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
 
# ─────────────────────────────────────────────
# 工具
# ─────────────────────────────────────────────
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
eval "$(mise activate zsh)"           # 语言版本管理器 — /mac/dev-toolchain
eval "$(zoxide init zsh --cmd cd)"
 
# ─────────────────────────────────────────────
# 提示符
# ─────────────────────────────────────────────
eval "$(starship init zsh)"

10. 验证

# 在新终端中
type starship              # /opt/homebrew/bin/starship
ZSH_AUTOSUGGEST_USE_ASYNC=true   # autosuggestions 已激活
echo $HISTSIZE             # 50000
 
# starship 提示符
cd ~/work/myrepo           # 提示符显示 git 分支 + 语言图标
 
# 建议
git c                      # 灰色建议 — Tab 接受
 
# fzf
history | head             # 输入 'git c' 然后 Ctrl+R — 模糊搜索

11. 故障排除

提示符显示乱码(▢▢)

缺少 Nerd Font。通过 brew 安装 font-jetbrains-mono-nerd-font 并更改终端字体。

command not found: starship

  • which starship 返回空 → brew 前缀是否在 PATH 中?
  • Apple Silicon:.zprofile 中应有 eval "$(/opt/homebrew/bin/brew shellenv)"

新终端启动缓慢(超过 1 秒)

  • 延迟加载 .zshrc 中的重型命令(如 nvm
  • zprof 分析:在顶部添加 zmodload zsh/zprof,底部添加 zprof | head -20

自动建议不显示

  • .zshrc 中的顺序:syntax-highlighting 在 autosuggestions 之后
  • ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE 可能与背景色太接近
  • 尝试 unset ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE 使用默认颜色

输入后语法高亮闪烁

  • 关闭 WezTerm/iTerm2 中的"快速渲染"
  • 或设置 ZSH_HIGHLIGHT_MAXLENGTH=300 禁用超长行的高亮

zsh: no matches found: *.txt

zsh 的 glob 默认很严格。要么在 .zshrc 中设置 setopt NULL_GLOB,要么转义:\*.txt


12. 下一步


参考资料

更新日志

  • 2026-05-16:首次发布。starship + 历史记录 + 自动建议 + 语法高亮 + fzf + 别名 + zoxide + 完整 .zshrc 示例 + 六个故障排除案例。