macOS 现代化终端环境从零搭建(Ghostty + Starship)
新机器到手、或者重装系统之后,照着这篇走一遍,就能把日常用的终端环境完整搭起来,不依赖记忆和零散笔记。
整体思路分三层,各管一摊:
- 软件交给 Homebrew,CLI 工具和 GUI 应用都靠它一条命令装齐;
- 配置交给 chezmoi,dotfiles 已托管在 GitHub,一行命令完成接管;
- 语言运行时交给 vfox,用到哪个版本再装,不提前锁死。
主流程走完即得到一套可用环境;标注「可选」的换源、Docker、第三方应用等不影响主线,按需取用即可。下面按实际操作顺序展开。
要装的东西
先过一遍清单,心里有数。CLI 工具一共 19 个:
| 包 | 用途 |
|---|---|
| chezmoi | dotfiles 管理,让配置跨机器同步 |
| starship | 跨 shell 的命令行提示符,展示目录、Git 状态、语言版本等 |
| zsh-autosuggestions | 根据历史输入灰色补全整条命令 |
| zsh-syntax-highlighting | 输入时实时语法高亮,命令对错一眼可辨 |
| zsh-completions | 补齐第三方命令的 Tab 补全 |
| fzf | 通用模糊查找,搜历史命令、文件、文本都靠它 |
| ripgrep | 更快的 grep 替代(命令为 rg ),递归搜文件内容 |
| fd | 更易用的 find 替代,文件查找更快 |
| bat | 带语法高亮和行号的 cat 替代 |
| yazi | 终端文件管理器,预览和批量操作全在键盘上完成 |
| vim | 终端文本编辑器 |
| gh | GitHub 官方 CLI,认证、PR、Issue 都能命令行处理 |
| lazygit | 终端里的 Git 界面,暂存、提交、变基更省事 |
| zellij | 终端复用器( tmux 替代),多面板多会话、断线可重连 |
| btop | 终端里的 CPU / 内存 / 网络监控 |
| sd | 更直观的 sed 替代,做字符串查找替换 |
| tlrc | tldr 客户端,快速查看命令的常用示例 |
| vfox | 多语言版本管理器,Go / Node 等版本按需切换 |
| chsrc | 给 Homebrew、npm 等一键切换国内镜像源 |
GUI 应用和字体共 2 个:
| 包 | 用途 |
|---|---|
| ghostty | 新一代终端模拟器,本文的默认终端 |
| font-jetbrains-mono | JetBrains Mono 等宽字体,终端和编辑器使用 |
系统初始化和开发基础
macOS 初始化的第一步,是把系统变成“可开发状态”,先完成 Xcode CLI 工具的初始化:
xcode-select --install
sudo xcodebuild -license accept初始化完成后,git、clang / gcc、make / ld 这些后续所有工具链依赖的基础就齐备了。
安装 Homebrew
Homebrew 是 macOS 上事实标准的包管理器,后面所有软件都通过它安装,接着把它装上:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"脚本执行完后,把 brew 注册到当前 shell。Apple Silicon 机器的固定安装路径是 /opt/homebrew:
eval "$(/opt/homebrew/bin/brew shellenv)"能正常输出版本号,就说明就绪了:
brew --version如果网络较慢,可以先给终端挂上代理,或者改用国内镜像源。
装齐 CLI 工具链
chezmoi、starship 和三个 zsh 插件构成终端基础体验,fzf、ripgrep、fd、bat、yazi 是日常高频命令,vim、gh、lazygit 负责编辑与 Git,zellij、btop、sd、tlrc 用于提效,vfox 和 chsrc 则是开发环境配置的好帮手。一条命令全部装齐,依赖由 Homebrew 自动补齐:
brew install chezmoi starship zsh-autosuggestions zsh-syntax-highlighting zsh-completions \
fzf ripgrep fd bat yazi vim gh lazygit zellij btop sd tlrc vfox chsrc用 chsrc 切换镜像源(可选)
国内网络访问 Homebrew、npm 等仓库较慢时,可以用刚装好的 chsrc 一键换源,它会先自动测速、再挑选最快的镜像:
chsrc list # 查看所有支持换源的目标
chsrc set brew # 给 Homebrew 换源
chsrc set npm # 给 npm 换源想恢复官方默认源用 chsrc reset brew;网络本身没问题的话,直接跳过本节即可。
安装 GUI 应用和字体
终端和等宽字体通过 cask 安装:
brew install --cask ghostty font-jetbrains-mono一行命令接管配置
软件只是壳,真正的使用习惯都在配置里。zsh、git、vim、ghostty、starship、yazi 等 dotfiles 都已经托管在 GitHub 的 chezmoi 仓库中,新机器上不用逐个手工还原,一条命令同时完成拉取和应用。
公开仓库直接用 HTTPS 地址:
chezmoi init --apply https://github.com/$GITHUB_USERNAME/dotfiles.git私密仓库走 SSH 地址,执行前确保新机的 SSH 公钥已经添加到 GitHub:
chezmoi init --apply git@github.com:$GITHUB_USERNAME/dotfiles.git把 $GITHUB_USERNAME 替换成自己的用户名即可。日后云端配置有更新,同样只要执行 chezmoi update。
还没有配置仓库时
如果此前没有把配置推送到 GitHub、暂时无仓库可拉,就按下面手动创建几个配置文件:用 vim 打开对应文件,把内容贴进去保存即可。
先用 vim 编辑 ~/.zshrc ( vim ~/.zshrc ),写入以下内容:
# Homebrew
eval "$(/opt/homebrew/bin/brew shellenv)"
# Additional completions
fpath=("$HOMEBREW_PREFIX/share/zsh-completions" $fpath)
autoload -Uz compinit && compinit
# zsh-autosuggestions
source "$HOMEBREW_PREFIX/share/zsh-autosuggestions/zsh-autosuggestions.zsh"
# alias
alias ls='ls -G'
alias ll='ls -lhG'
alias la='ls -lahG'
# yazi
function y() {
local tmp cwd; tmp="$(mktemp -t "yazi-cwd.XXXXXX")"
command yazi "$@" --cwd-file="$tmp"
IFS= read -r -d '' cwd < "$tmp"
[ "$cwd" != "$PWD" ] && [ -d "$cwd" ] && builtin cd -- "$cwd" || builtin true
command rm -f -- "$tmp"
}
# fzf
source <(fzf --zsh)
# bat
export BAT_THEME="Catppuccin Mocha"
# starship
eval "$(starship init zsh)"
# vfox
eval "$(vfox activate zsh)"
# zsh-syntax-highlighting
# need to load this at the very end of .zshrc
source "$HOMEBREW_PREFIX/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"提示符主题用 starship 官方预设直接生成一份 Catppuccin Powerline 配置:
mkdir -p ~/.config
starship preset catppuccin-powerline -o ~/.config/starship.toml再编辑 Ghostty 配置 ~/.config/ghostty/config.ghostty (目录不存在先执行 mkdir -p ~/.config/ghostty ),写入以下内容:
# ============================================
# Ghostty Config
# 重载快捷键:Cmd + Shift + ,
# ============================================
# --- Typography ---
#font-family = "Maple Mono NF CN"
font-family = "JetBrains Mono"
font-size = 14
font-thicken = true
adjust-cell-height = 2
# --- Theme and Colors ---
theme = iTerm2 Smoooooth
# --- Window and Appearance ---
background-opacity = 0.85
background-blur = macos-glass-clear
macos-titlebar-style = transparent
window-padding-x = 10
window-padding-y = 8
window-save-state = always
window-theme = auto
# --- Cursor ---
cursor-style = bar
cursor-style-blink = true
cursor-opacity = 0.8
# --- Mouse ---
mouse-hide-while-typing = true
copy-on-select = clipboard
# --- Quick Terminal ---
quick-terminal-position = top
quick-terminal-screen = mouse
quick-terminal-autohide = true
quick-terminal-animation-duration = 0.15
# --- Security ---
clipboard-paste-protection = true
clipboard-paste-bracketed-safe = true
# --- Shell Integration ---
shell-integration = detect
# 全局快捷键:Ctrl+`
keybind = global:ctrl+grave_accent=toggle_quick_terminal
# 下拉窗口配置
quick-terminal-position = top
quick-terminal-screen = mouse
quick-terminal-autohide = true
quick-terminal-animation-duration = 0.15
# macoS 关闭窗口不退出App,后台常驻,保证下次秒开
quit-after-last-window-closed = false
# macos option 视为 alt
macos-option-as-alt = true
# --- Performance ---
scrollback-limit = 25000000
# --- Keybindings ---
# Tabs
keybind = cmd+t=new_tab
keybind = cmd+shift+left=previous_tab
keybind = cmd+shift+right=next_tab
keybind = cmd+w=close_surface
# Splits
keybind = cmd+d=new_split:right
keybind = cmd+shift+d=new_split:down
keybind = cmd+alt+left=goto_split:left
keybind = cmd+alt+right=goto_split:right
keybind = cmd+alt+up=goto_split:top
keybind = cmd+alt+down=goto_split:bottom
# Font size
keybind = cmd+plus=increase_font_size:1
keybind = cmd+minus=decrease_font_size:1
keybind = cmd+zero=reset_font_size
# Splits management
keybind = cmd+shift+e=equalize_splits
keybind = cmd+shift+f=toggle_split_zoom写完执行 exec zsh 让 shell 配置生效、重启 Ghostty 让终端配置生效;等以后把配置 纳入 chezmoi 管理,再改用上面的 chezmoi init --apply 即可。
安装 Vim 插件
.vimrc 已经随上一步恢复,但插件管理器 vim-plug 本身、以及插件实体还需要单独安装。先下载插件管理器:
curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim再让它按照 .vimrc 里的声明,把插件批量装好:
vim +'PlugInstall --sync' +qa让配置生效
重载一次 shell,starship 提示符、fzf、vfox 等初始化逻辑就全部生效了:
exec zsh语言运行时(vfox,按需安装)
Go、Node 这类运行时不通过 Homebrew 锁死版本,统一交给 vfox 管理:需要哪个版本就装哪个,不同项目之间也能随时切换。以 Go 和 Node 为例:
vfox add golang
vfox install golang@1.27.0 && vfox use -g golang@1.27.0
vfox add nodejs
vfox install nodejs@24.20.0 && vfox use -g nodejs@24.20.0vfox add 添加语言插件,install 安装指定版本,use -g 设为全局默认;vfox list 可以随时查看已装版本,其他语言照这个模式套用即可。
可选应用推荐(可选)
下面这些是日常顺手的 macOS 应用,不属于必需环境,按喜好挑着装即可:
| 应用 | 用途 |
|---|---|
| visual-studio-code | 主流开源代码编辑器,插件生态丰富 |
| google-chrome | Chrome 浏览器,日常浏览与开发调试 |
| docker-desktop | Docker 桌面版,自带 docker 命令行,需要容器环境时装 |
| input-source-pro | 按当前 App 自动切换中英文输入法 |
| maccy | 剪贴板历史管理,随时调出之前复制的内容 |
| pixpin | 截图 / 贴图 / 标注,截图可钉在屏幕上 |
| stats | 菜单栏实时显示 CPU、内存、网络、温度 |
| mac-mouse-fix | 给第三方鼠标加按键手势与平滑滚动 |
| only-switch | 菜单栏一键开关深色模式、隐藏桌面等 |
| iina | 基于 mpv 的现代视频播放器,格式支持全 |
| keka | 压缩 / 解压,覆盖常见与冷门归档格式 |
| pictureview | 轻量图片查看器,流畅浏览本地图片 |
| macs-fan-control | 设定并监控苹果电脑的风扇转速 |
| pearcleaner | 彻底卸载应用并清理残留文件 |
| onyx | 系统维护、缓存清理与权限修复 |
| cc-switch | 在多个 AI 编码助手配置间快速切换 |
| finetune | 按应用单独调音量、均衡器和音频路由 |
想批量安装,一条命令即可(也可以只挑其中几个):
brew install --cask visual-studio-code google-chrome docker-desktop input-source-pro maccy pixpin \
stats mac-mouse-fix only-switch iina keka pictureview macs-fan-control \
pearcleaner onyx cc-switch finetune命令汇总
最后把所有命令按执行顺序汇总一份,方便直接复制:
# Xcode CLI
xcode-select --install
sudo xcodebuild -license accept
# Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
eval "$(/opt/homebrew/bin/brew shellenv)"
# CLI 工具 + GUI / 字体
brew install chezmoi starship zsh-autosuggestions zsh-syntax-highlighting zsh-completions \
fzf ripgrep fd bat yazi vim gh lazygit zellij btop sd tlrc vfox chsrc
brew install --cask ghostty font-jetbrains-mono
# 接管云端配置(公开用 HTTPS,私密用 SSH)
# chezmoi init --apply https://github.com/$GITHUB_USERNAME/dotfiles.git
# chezmoi init --apply git@github.com:$GITHUB_USERNAME/dotfiles.git
# Vim 插件,然后重载 shell
curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
vim +'PlugInstall --sync' +qa
exec zsh