## iTerm2 介绍
iTerm2 是一款功能强大的终端工具,也可以说是 Terminal 的替代品,也可以说是 iTerm 的后继产品。它适用于 macOS 10.12 或更高版本的 macOS。
它支持分窗口操作、自动补齐、粘贴历史、回放功能、全屏等功能,是一款非常强大、非常值得推荐的终端工具。
## 安装 iTerm2
- 可以去 [iTerm2 官网](https://www.iterm2.com/downloads.html),下载并安装。
- 可以直接使用 Homebrew 进行安装:
```javascript
$ brew cask install iterm2
```
**注意**:
如果你的 Mac 上没有安装 brew 命令,请先执行如下命令进行安装:
```javascript
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
```
## 配置 iTerm2 主题
iTerm2 最常用的主题是 Solarized Dark theme,下载地址:[https://ethanschoonover.com/solarized](https://ethanschoonover.com/solarized)
下载的是压缩文件,你先解压一下,然后打开 iTerm2,按 【Command + , 】键,打开 Preferences 配置界面,然后 Profiles -> Colors -> Color Presets -> Import,选择刚才解压的 solarized -> iterm2-colors-solarized -> Solarized Dark.itermcolors 文件,导入成功,最后选择 Solarized Dark 主题,就可以了,如下所示:

该工具主题有很多,可以根据自己的爱好进行选择。
## 配置 oh-my-zsh
oh-my-zsh 是对主题的进一步扩展,Github 地址:[https://github.com/ohmyzsh/ohmyzsh](https://github.com/ohmyzsh/ohmyzsh)
你可以通过 curl 或 wget 命令进行安装:
- curl 安装
```javascript
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
```
- wget 安装
```javascript
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
```
例如,我这里通过 curl 命令进行安装:
```javascript
$ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Cloning Oh My Zsh...
xcode-select: note: no developer tools were found at '/Applications/Xcode.app', requesting install. Choose an option in the dialog to download the command line developer tools.
Error: git clone of oh-my-zsh repo failed
Eason:Library linan$ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Cloning Oh My Zsh...
Cloning into '/Users/linan/.oh-my-zsh'...
remote: Enumerating objects: 1089, done.
remote: Counting objects: 100% (1089/1089), done.
remote: Compressing objects: 100% (1030/1030), done.
remote: Total 1089 (delta 23), reused 874 (delta 19), pack-reused 0
Receiving objects: 100% (1089/1089), 716.00 KiB | 65.00 KiB/s, done.
Resolving deltas: 100% (23/23), done.
Looking for an existing zsh config...
Using the Oh My Zsh template file and adding it to ~/.zshrc.
Time to change your default shell to zsh:
Do you want to change your default shell to zsh? [Y/n] y
Changing the shell...
Changing shell for linan.
Password for linan:
Shell successfully changed to '/bin/zsh'.
__ __
____ / /_ ____ ___ __ __ ____ _____/ /_
/ __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \
/ /_/ / / / / / / / / / / /_/ / / /_(__ ) / / /
\____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/
/____/ ....is now installed!
Please look over the ~/.zshrc file to select plugins, themes, and options.
p.s. Follow us on https://twitter.com/ohmyzsh
p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh
```
安装好之后,需要把 zsh 设置为当前用户的默认 shell(这样新建命令的时候才会使用 zsh):
```javascript
$ chsh -s /bin/zsh
```
然后,我们用 vim 命令编辑 ~/.zshrc 文件,将主题配置修改为 ZSH_THEME="agnoster"。
```javascript
$ vim ~/.zshrc
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH="/Users/linan/.oh-my-zsh"
# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
ZSH_THEME="agnoster" // 修改主题
```
agnoster 是比较常用的 zsh 主题之一,你可以挑选你喜欢的主题,zsh 主题列表:[https://github.com/ohmyzsh/ohmyzsh/wiki/themes](https://github.com/ohmyzsh/ohmyzsh/wiki/themes)
## 配置 Meslo 字体
使用上面的主题,需要 Meslo 字体支持,要不然会出现乱码的情况,字体下载地址:[Meslo LG M Regular for Powerline.ttf](https://github.com/powerline/fonts/blob/master/Meslo%20Slashed/Meslo%20LG%20M%20Regular%20for%20Powerline.ttf)
下载好之后,直接在 macOS 中双击安装即可。
然后打开 iTerm2,按【Command + , 】键,打开 Preferences 配置界面,然后 Profiles -> Text -> Font -> Chanage Font,选择 Meslo LG M Regular for Powerline 字体。

当然,如果你觉得默认的12px字体大小不合适,可以自己进行修改。
另外,VS Code 的终端字体,也需要进行配置,打开 VS Code,按 Command + ,键,打开用户配置,搜索 fontFamily,然后将右边的配置增加"terminal.integrated.fontFamily": "Meslo LG M for Powerline",示例:

## 声明高亮
效果就是上面截图的那样,特殊命令和错误命令,会有高亮显示。
使用 Homebrew 安装:
```javascript
$ brew install zsh-syntax-highlighting
```
安装成功之后,用 vim 命令编辑 ~/.zshrc 文件,在最后一行增加下面配置:
```javascript
...................
# Compilation flags
# export ARCHFLAGS="-arch x86_64"
# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
```
保存后,执行变量生效命令:
```javascript
source /etc/profile
```
## 自动补齐
这个功能是非常实用的,可以方便我们快速的敲命令。
配置步骤,先克隆 zsh-autosuggestions 项目,到指定目录:
```javascript
$ git clone https://github.com/zsh-users/zsh-autosuggestions ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions
```
然后,用 vim 命令编辑 ~/.zshrc 文件,找到 plugins 配置,增加 zsh-autosuggestions 插件:
```javascript
# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder
# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git zsh-autosuggestions)
```
> 注意:
上面声明高亮,如果配置不生效的话,可以在 plugins 配置,再增加 zsh-syntax-highlighting 插件试试。
有时候因为自动填充的颜色和背景颜色很相似,以至于自动填充没有效果,我们可以手动更改下自动填充的颜色配置,我修改的颜色值为:3a717f,示例:

## 左右键跳转
主要是按住 option + → or ← 键,在命令的开始和结尾跳转切换,原本是不生效的,需要手动开启下。
打开 iTerm2,按 Command + , 键,打开 Preferences 配置界面,然后 Profiles → Keys → Load Preset... → Natural Text Editing,就可以了。
## iTerm2 快速隐藏和显示
这个功能也非常使用,就是通过快捷键,可以快速的隐藏和打开 iTerm2,示例配置快捷键:( Commond + . ):

## iTerm2 隐藏用户名和主机名
有时候我们的用户名和主机名太长,比如我的 pengzhenjin@MacBook-Pro,终端显示的时候会很不好看,我们可以手动去除。用 vim 命令编辑 ~/.zshrc 文件,增加 DEFAULT_USER="pengzhenjin" 配置,如:
```javascript
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH="/Users/user/.oh-my-zsh"
# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="agnoster"
DEFAULT_USER="pengzhenjin"
```
## iTerm2 配置代理
用 vim 命令编辑 ~/.zshrc 文件,增加下面配置(使用的 shadowsocks):
```javascript
# proxy list
alias proxy='export all_proxy=socks5://127.0.0.1:1086'
alias unproxy='unset all_proxy'
```
iTerm2 需要新建标签页,才有效果:
```javascript
$ proxy
$ curl ip.cn
当前 IP:185.225.14.5 来自:美国
$ unproxy
$ curl ip.cn
当前 IP:115.236.186.130 来自:浙江省杭州市 电信
```
我们可以测试下:
```javascript
$ curl https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
<html>
<head>
<title>Directory listing for /yum/repos/kubernetes-el7-x86_64/</title>
</head>
<body>
<h2>Index of /yum/repos/kubernetes-el7-x86_64/</h2>
<p></p>
<a href="/yum/repos/kubernetes-el7-x86_64/repodata">repodata</a><br />
</body>
</html>
```
## iTerm2 常用命令快捷键
- 新建命令窗口:command + t
- 关闭命令窗口:command + w
- 切换命令窗口:command + 左右方向键
- 查找: command + f
- 垂直分屏:command + d
- 水平分屏:command + shift + d
- 自动补齐: command +;
- 命令历史: command + shift + h
- 命令回放: command + alt + b
- 光标移动到行首: ctrl + a
- 光标移动到行末: ctrl + e
- 前移一个字符: ctrl + f
- 后退一个字符: ctrl + b
- 清屏: ctrl + l
- 显示历史命令: ctrl + p
- 删除当前字符: ctrl + d
- 删除前一字符: ctrl + h
- 删除光标之前的字符: ctrl + w
- 删除光标之前的整行: ctrl + n
- 删除光标之后整行: ctrl + k
## 参考资料
[iTerm2终端工具在Mac OS上使用详解](https://www.cnblogs.com/easonscx/p/11678480.html)

Mac 最强大的终端工具之 iTerm2 详解