macOS 系列之 iTerm2 + Zsh
# macOS 系列之 iTerm2 + Oh My Zsh
# 📖. 前言
iTerm2 应该已经是大部分 Mac 开发者必装的终端机软体,本文整理一些 iTerm2 的基本资讯、常规设定和操作。
# iTerm2 是什么?
虽然 macOS 内建有自己的终端机工具 terminal.app
,但 iTerm2 是更好用的替代方案。
iTerm2 官网 (opens new window) 对自己的介绍
iTerm2 is a replacement for Terminal and the successor to iTerm. It works on Macs with macOS 10.14 or newer.
iTerm2 brings the terminal into the modern age with features you never knew you always wanted.
因为 powerline 以及 homebrew 均需要安装 command lint tool,网络条件优越的同学在执行本文下面内容之前,可以先安装 XCode 并打开运行一次(会初始化安装 components), 省去以后再 iTerm2 中的等待时间。
另外,git 也是必要的,各位可以自行下载安装,除了网络没有任何坑 → 下载Git (opens new window)
介于此,本文默认各位同学已经安装了 git 环境和 xcode(command line tools),遇到提示找不到 git 命令或需要安装 command line tool 的地方,文中不再赘述了。
# 一. 安装 iTerm2
根据你的习惯,有两种方法:
# 1. 到官网下载:
- 到 官网下载 zip 档 (opens new window)
- 解压缩 zip 档,得到
iTerm.app
档案 - 把
iTerm.app
档复制到 Applications 资料夹里
# 2. 通过 homebrew 安装
我比较推荐用这个方法,让应用程序更好管理。
通过 homebrew 安装 iTerm2 的命令:
brew cask install iterm2
如果没安装过 homebrew,可以参照另一篇文章 《Mac 如何安装 Homebrew》 进行安装。
安装完成后,在 /bin
目录下会多出一个 zsh 的文件。
Mac 系统默认使用 dash
作为终端,可以使用命令修改默认使用 zsh
:
chsh -s /bin/zsh
如果想修改回默认 dash
,同样使用 chsh
命令即可:
chsh -s /bin/bash
# 二. 安装 Oh my zsh
安装方法有两种,可以使用 curl
或 wget
,看自己环境或喜好:(本人使用了第一种):
# curl 安装方式
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
2
# wget 安装方式
sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
2
安装命令和安装完成后的截图:
oh-my-zsh开源地址: https://github.com/robbyrussell/oh-my-zsh (opens new window)
切换 shell
chsh -s /bin/bash
chsh -s /bin/zsh
2
3
# 三. 安装 PowerLine
PowerLine 是 agnoster 主题的依赖,具体可以在 repo 里查询到:https://github.com/agnoster/agnoster-zsh-theme (opens new window)
PowerLine官网 (opens new window)
安装 powerline 的方式依然简单,也只需要一条命令:
pip install powerline-status --user
没有安装 pip 的同学可能会碰到 zsh: command not found:pip
使用命令安装 pip 即可:
sudo easy_install pip
安装后再次执行安装 powerline 的命令即可。
# 四. 安装 PowerFonts
安装字体库首先需要将项目 git clone 至本地,然后执行源码中的 install.sh
。
在你习惯的位置新建一个文件夹,如:~/Desktop/OpenSource
在此文件夹下执行 git clone
命令:
# git clone
git clone https://github.com/powerline/fonts.git --depth=1
# cd to folder
cd fonts
# run install shell
./install.sh
2
3
4
5
6
7
8
执行结果如下:
安装好字体库之后,我们来设置 iTerm2 的字体,具体的操作是 iTerm2 → Preferences → Profiles → Text,在 Font 区域训中 「Change Font」,然后找到 「Meslo LG」字体。有 L、M、S 可选,看个人喜好:
# 五. 安装配色方案
配色方案在使用 VIM 或 Colorful Log 时会变得非常有用,同时界面也不会一片黑绿一样死板。
同样使用 git clone 的方式下载源码进行安装:
cd ~/Desktop/OpenSource
git clone https://github.com/altercation/solarized
cd solarized/iterm2-colors-solarized/
open .
2
3
4
5
6
7
在打开的 Finder 窗口中,双击 Solarized Dark.itermcolors 和 Solarized Light.itermcolors 即可安装明暗两种配色:
再次进入 iTerm2 → Preferences → Colors → Color Presets 中根据个人喜好选择这两种配色中的一种即可:
# 六. 安装主题
下载 agnoster 主题,执行安装脚本:
cd ~/Desktop/OpenSource
git clone https://github.com/fcamblor/oh-my-zsh-agnoster-fcamblor.git
cd oh-my-zsh-agnoster-fcamblor/
./install
2
3
4
5
6
7
执行上面的命令会将主题拷贝到 oh my zsh 的 themes 中:
拷贝完成后,执行命令打开 zshrc 配置文件,将 ZSH_THEME
后面的字段改为 agnoster
。
vi ~/.zshrc
修改完成后按一下 esc 调出 vi 命令,输入 :wq
保存并退出 vi 模式。
此时,command + Q
或 source 配置文件后,iTerm2 变了模样:
# 七. 安装高亮插件
这是 oh my zsh 的一个插件,安装方式与 theme 大同小异:
cd ~/.oh-my-zsh/custom/plugins/
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
vi ~/.zshrc
2
3
4
5
这时我们再打开 zshrc 文件进行编辑。找到 plugins,此时 plugins 中应该已经有了 git,我们需要把高亮插件也加上:
请务必保证插件顺序,zsh-syntax-highlighting 必须在最后一个。
然后在文件的最后一行添加:
source ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
按一下 esc 调出 vi 命令:输入 :wq
保存并退出 vi m模式。
执行命令使刚才的修改生效:
source ~/.zshrc
至此大功告成。
# 八. 可选择、命令补全
跟代码高亮的安装方式一样,这也是一个 zsh 的插件,叫做 zsh-autosuggestion
,用于命令建议和补全。
cd ~/.oh-my-zsh/custom/plugins/
git clone https://github.com/zsh-users/zsh-autosuggestions
vi ~/.zshrc
2
3
4
5
找到 plugins,加上这个插件:
然后在文件的最后一行添加:
source ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
插件效果:
有同学说,补全命令的字体不太清晰,与背景色太过相近,其实可以自己调整一下字体颜色。
Preferences → Profiles → Colors
中有 Foreground 是标准字体颜色,ANSI Colors 中的 Bright 的第一个是补全的字体颜色。