macOS 终端利器 iTerm2 配置指南
Posted on Mon 26 May 2025 in 开发工具
iTerm2 是 macOS 上最强大的终端工具之一,本文将介绍如何安装和配置 iTerm2,让它成为你的得力助手。
安装步骤
1. 安装 iTerm2
有两种安装方式: - 直接下载:从 iTerm2 官网 下载,解压后拖到 Applications 目录 - 使用 Homebrew:
brew install --cask iterm2
2. 配置主题
- 下载 Solarized Dark theme:http://ethanschoonover.com/solarized
- 打开 iTerm2 偏好设置:
Command + , - 进入 Profiles -> Colors -> Color Presets -> Import
- 导入下载的
Solarized Dark.itermcolors文件 - 选择 Solarized Dark 主题
3. 安装 Oh My Zsh
- 一键安装:
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
- 设置 Zsh 为默认 Shell:
chsh -s /bin/zsh
- 配置主题:
vim ~/.zshrc # 修改 ZSH_THEME="agnoster"
4. 配置字体
- 下载并安装 Meslo 字体:Meslo LG M Regular for Powerline.ttf
- iTerm2 配置:
- 打开偏好设置:
Command + , - Profiles -> Text -> Font -> Change Font
- 选择 "Meslo LG M Regular for Powerline" 字体
5. 增强功能配置
5.1 语法高亮
# 安装
brew install zsh-syntax-highlighting
# 配置
echo "source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ~/.zshrc
5.2 自动补全
# 安装
git clone https://github.com/zsh-users/zsh-autosuggestions ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions
# 配置
vim ~/.zshrc
# 在 plugins 中添加 zsh-autosuggestions
plugins=(git zsh-autosuggestions zsh-syntax-highlighting)
5.3 快捷键配置
- 左右键跳转:
- 打开偏好设置:
Command + , -
Profiles → Keys → Load Preset... → Natural Text Editing
-
快速显示/隐藏:
- 打开偏好设置:
Command + , - Keys → Hotkey
- 设置快捷键(推荐 Command + .)
5.4 隐藏用户名和主机名
# 在 ~/.zshrc 中添加
DEFAULT_USER="heyaohua"
5.5 配置代理
# 在 ~/.zshrc 中添加
alias proxy='export all_proxy=socks5://127.0.0.1:1086'
alias unproxy='unset all_proxy'
使用技巧
- 分屏操作:
- 垂直分屏:Command + D
- 水平分屏:Command + Shift + D
-
切换分屏:Command + [ 或 Command + ]
-
标签页操作:
- 新建标签页:Command + T
- 关闭标签页:Command + W
-
切换标签页:Command + 数字 或 Command + 左右方向键
-
其他常用快捷键:
- 清屏:Command + K
- 查找:Command + F
- 复制:Command + C
- 粘贴:Command + V
- 全屏:Command + Enter
常见问题解决
- 如果自动补全颜色不明显:
- 打开偏好设置:
Command + , - Profiles -> Colors
-
修改 ANSI Colors 中的颜色值(推荐:586e75)
-
如果出现乱码:
- 确保已安装 Powerline 字体
- 检查 iTerm2 的字体设置是否正确
- 重新加载 zsh 配置:
source ~/.zshrc