macOS 终端利器 iTerm2 配置指南

Posted on Mon 26 May 2025 in 开发工具

iTerm2 是 macOS 上最强大的终端工具之一,本文将介绍如何安装和配置 iTerm2,让它成为你的得力助手。

安装步骤

1. 安装 iTerm2

有两种安装方式: - 直接下载:从 iTerm2 官网 下载,解压后拖到 Applications 目录 - 使用 Homebrew:

brew install --cask iterm2

2. 配置主题

  1. 下载 Solarized Dark theme:http://ethanschoonover.com/solarized
  2. 打开 iTerm2 偏好设置:Command + ,
  3. 进入 Profiles -> Colors -> Color Presets -> Import
  4. 导入下载的 Solarized Dark.itermcolors 文件
  5. 选择 Solarized Dark 主题

3. 安装 Oh My Zsh

  1. 一键安装:
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
  1. 设置 Zsh 为默认 Shell:
chsh -s /bin/zsh
  1. 配置主题:
vim ~/.zshrc  # 修改 ZSH_THEME="agnoster"

4. 配置字体

  1. 下载并安装 Meslo 字体:Meslo LG M Regular for Powerline.ttf
  2. iTerm2 配置:
  3. 打开偏好设置:Command + ,
  4. Profiles -> Text -> Font -> Change Font
  5. 选择 "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 快捷键配置

  1. 左右键跳转:
  2. 打开偏好设置:Command + ,
  3. Profiles → Keys → Load Preset... → Natural Text Editing

  4. 快速显示/隐藏:

  5. 打开偏好设置:Command + ,
  6. Keys → Hotkey
  7. 设置快捷键(推荐 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'

使用技巧

  1. 分屏操作:
  2. 垂直分屏:Command + D
  3. 水平分屏:Command + Shift + D
  4. 切换分屏:Command + [ 或 Command + ]

  5. 标签页操作:

  6. 新建标签页:Command + T
  7. 关闭标签页:Command + W
  8. 切换标签页:Command + 数字 或 Command + 左右方向键

  9. 其他常用快捷键:

  10. 清屏:Command + K
  11. 查找:Command + F
  12. 复制:Command + C
  13. 粘贴:Command + V
  14. 全屏:Command + Enter

常见问题解决

  1. 如果自动补全颜色不明显:
  2. 打开偏好设置:Command + ,
  3. Profiles -> Colors
  4. 修改 ANSI Colors 中的颜色值(推荐:586e75)

  5. 如果出现乱码:

  6. 确保已安装 Powerline 字体
  7. 检查 iTerm2 的字体设置是否正确
  8. 重新加载 zsh 配置:source ~/.zshrc