我的 iTerm2 终端配置
6 分钟
本指南适用于 macOS 系统,其余系统笔者不是很清楚,但应该也有相似的方案
前置要求
# 安装 Homebrew (如未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 安装 iTerm2
brew install --cask iterm2
一、安装 Oh My Zsh
Oh My Zsh 是一个管理 Zsh 配置的框架,提供丰富的主题和插件。
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
二、安装 Powerlevel10k 主题
Powerlevel10k 是一个高度可定制的 Zsh 主题,支持 Git 状态、命令执行时间等信息显示。
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
三、安装 Nerd Font 字体
Nerd Font 包含大量图标,是 Powerlevel10k 正常显示的必要条件。
brew install --cask font-meslo-lg-nerd-font
iTerm2 设置字体:
- 打开 iTerm2 → Settings (⌘,)
- Profiles → Text → Font
- 选择
MesloLGS Nerd Font或MesloLGM Nerd Font - 建议字号:14-16
四、安装 Zsh 插件
4.1 zsh-syntax-highlighting(语法高亮)
命令输入时实时高亮,正确命令显示绿色,错误显示红色。
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
4.2 zsh-autosuggestions(自动补全建议)
根据历史命令自动提示,按 → 键接受建议。
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
五、安装实用命令行工具
brew install eza bat fzf
| 工具 | 说明 |
|---|---|
| eza | 替代 ls,支持图标、颜色、Git 状态 |
| bat | 替代 cat,支持语法高亮、行号 |
| fzf | 模糊搜索工具,增强 Ctrl+R 历史搜索 |
六、配置 .zshrc
编辑 ~/.zshrc 文件:
vim ~/.zshrc
# 或
code ~/.zshrc
6.1 设置主题
找到 ZSH_THEME 行,修改为:
ZSH_THEME="powerlevel10k/powerlevel10k"
6.2 启用插件
找到 plugins 行,修改为:
plugins=(git zsh-syntax-highlighting zsh-autosuggestions z fzf)
插件说明:
git- Git 命令别名和补全zsh-syntax-highlighting- 语法高亮zsh-autosuggestions- 自动建议z- 快速跳转目录(根据访问频率)fzf- 模糊搜索集成
6.3 添加实用别名
在文件末尾添加:
# ========== Custom Aliases ==========
# Better ls with eza
alias ls='eza --icons --group-directories-first'
alias ll='eza -l --icons --group-directories-first'
alias la='eza -la --icons --group-directories-first'
alias lt='eza --tree --level=2 --icons'
# Better cat with bat
alias cat='bat --paging=never'
# fzf configuration
export FZF_DEFAULT_OPTS='--height 40% --layout=reverse --border'
6.4 应用配置
source ~/.zshrc
七、配置 Powerlevel10k
首次启动终端会自动运行配置向导,按提示选择样式。
如需重新配置:
p10k configure
配置文件位于 ~/.p10k.zsh,可手动编辑微调。
八、iTerm2 美化设置
8.1 外观设置
| 设置路径 | 推荐值 |
|---|---|
| Appearance → General → Theme | Minimal |
| Profiles → Window → Transparency | 10-15% |
| Profiles → Window → Blur | 开启,值 10-20 |
| Profiles → Window → Style | Normal 或 No Title Bar |
8.2 配色方案
推荐配色:
- Dracula: https://draculatheme.com/iterm/
- Catppuccin: https://github.com/catppuccin/iterm
- One Dark: https://github.com/one-dark/iterm-one-dark-theme
- Tokyo Night: https://github.com/enkia/tokyo-night-vscode-theme
导入方法:
- 下载
.itermcolors文件 - iTerm2 → Settings → Profiles → Colors → Color Presets → Import
- 选择导入的配色方案
8.3 快捷键优化
添加 Option + 左右箭头 跳词功能:
- Settings → Profiles → Keys → Key Mappings → + (添加)
- Option + ← : Send Escape Sequence
b - Option + → : Send Escape Sequence
f
九、一键安装脚本
将以下内容保存为 setup-iterm.sh,在新电脑上一键执行:
#!/bin/bash
echo "🚀 开始配置 iTerm2 环境..."
# 安装 Oh My Zsh
if [ ! -d "$HOME/.oh-my-zsh" ]; then
echo "📦 安装 Oh My Zsh..."
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
fi
# 安装 Powerlevel10k
if [ ! -d "${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k" ]; then
echo "🎨 安装 Powerlevel10k..."
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
fi
# 安装字体
echo "🔤 安装 Nerd Font..."
brew install --cask font-meslo-lg-nerd-font
# 安装插件
echo "🔌 安装 Zsh 插件..."
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting 2>/dev/null
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions 2>/dev/null
# 安装工具
echo "🛠️ 安装命令行工具..."
brew install eza bat fzf
# 配置 .zshrc
echo "⚙️ 配置 .zshrc..."
sed -i '' 's/ZSH_THEME="robbyrussell"/ZSH_THEME="powerlevel10k\/powerlevel10k"/' ~/.zshrc
sed -i '' 's/plugins=(git)/plugins=(git zsh-syntax-highlighting zsh-autosuggestions z fzf)/' ~/.zshrc
# 添加别名
cat >> ~/.zshrc << 'EOF'
# ========== Custom Aliases ==========
alias ls='eza --icons --group-directories-first'
alias ll='eza -l --icons --group-directories-first'
alias la='eza -la --icons --group-directories-first'
alias lt='eza --tree --level=2 --icons'
alias cat='bat --paging=never'
export FZF_DEFAULT_OPTS='--height 40% --layout=reverse --border'
EOF
echo "✅ 配置完成!请重启终端并运行 p10k configure"
运行方式:
chmod +x setup-iterm.sh
./setup-iterm.sh
十、常用快捷键
| 快捷键 | 功能 |
|---|---|
Ctrl + R | fzf 历史命令搜索 |
Ctrl + T | fzf 文件搜索 |
Alt + C | fzf 目录跳转 |
→ | 接受自动补全建议 |
z <目录关键词> | 快速跳转常用目录 |
效果预览
配置完成后,终端将具备:
- ✨ 美观的 Powerlevel10k 主题
- 🎨 丰富的图标显示
- 🔍 智能命令补全和历史搜索
- 📝 实时语法高亮
- 📁 更好的文件列表展示
最后更新:2025-12-29