一款 macOS 上的终端模拟器iTerm2,程序员在 Mac 上必装的软件之一
次访问标签:mac
iTerm2 介绍
iTerm2 是一款功能强大的终端工具,也可以说是 Terminal 的替代品,也可以说是 iTerm 的后继产品。它适用于 macOS 10.12 或更高版本的 macOS。
它支持分窗口操作、自动补齐、粘贴历史、回放功能、全屏等功能,是一款非常强大、非常值得推荐的终端工具。
安装 iTerm2
官网下载包安装:http://iterm2.com/
使用Homwbrew安装:
brew install iterm2
iTerm2配置主题
iTerm2 最常用的主题是 Solarized Dark theme,下载地址: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
你可以通过 curl 或 wget 命令进行安装:
curl 安装
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
wget 安装
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
例如,我这里通过 curl 命令进行安装:
$ 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):
$ chsh -s /bin/zsh
然后,我们用 vim 命令编辑 ~/.zshrc 文件,将主题配置修改为 ZSH_THEME=”agnoster”。
$ 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
配置 Meslo 字体
使用上面的主题,需要 Meslo 字体支持,要不然会出现乱码的情况,字体下载地址:Meslo LG M Regular for Powerline.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 安装:
$ brew install zsh-syntax-highlighting
安装成功之后,用 vim 命令编辑 ~/.zshrc 文件,在最后一行增加下面配置:
...................
# 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
保存后,执行变量生效命令:
source /etc/profile
自动补齐
这个功能是非常实用的,可以方便我们快速的敲命令。
配置步骤,先克隆 zsh-autosuggestions 项目,到指定目录:
$ git clone https://github.com/zsh-users/zsh-autosuggestions ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions
然后,用 vim 命令编辑 ~/.zshrc 文件,找到 plugins 配置,增加 zsh-autosuggestions 插件:
# 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)
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