iTerm2 + oh-my-zsh = 好用的終端機

前言

iTerm2 是 macOS 下好用的 terminal 軟體,在 iTerm2 下安裝 oh-my-zsh 以打造個人專屬酷炫好用的 zsh command line 環境 :)

安裝 iTerm2

1
2
3
4
5
6
7
8
9
$ brew tap caskroom/cask
$ brew cask instal iterm2
==> Satisfying dependencies
==> Downloading https://iterm2.com/downloads/stable/iTerm2-3_2_0.zip
######################################################################## 100.0%
==> Verifying SHA-256 checksum for Cask 'iterm2'.
==> Installing Cask iterm2
==> Moving App 'iTerm.app' to '/Applications/iTerm.app'.
🍺 iterm2 was successfully installed!

設定 iTerm2

設定 Terminal 顏色

1
Preferences > Profiles > Terminal > Report Terminal Type

設定成 xterm-256color

修改 color scheme

mbadolato/iTerm2-Color-Schemes 下載 Solarized Dark - Patched.itermcolors,然後再從

1
Preferences > Profiles > Colors > Color Presets...

import… 將它載入,再從 Color Presets… 選定 Solarized Dark - Patched.itermcolors 作為 iTerm2 的 theme。

字型管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ brew tap caskroom/fonts

Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
pulumi

==> Tapping homebrew/cask-fonts
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask-fonts'...
remote: Counting objects: 1198, done.
remote: Compressing objects: 100% (729/729), done.
remote: Total 1198 (delta 762), reused 571 (delta 466), pack-reused 0
Receiving objects: 100% (1198/1198), 228.83 KiB | 134.00 KiB/s, done.
Resolving deltas: 100% (762/762), done.
Tapped 1175 casks (1,215 files, 1MB).

安裝 powerline font 字型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$ brew cask install font-sourcecodepro-nerd-font
Updating Homebrew...
==> Tapping homebrew/cask
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask'...
remote: Counting objects: 4139, done.
remote: Compressing objects: 100% (4126/4126), done.
remote: Total 4139 (delta 25), reused 624 (delta 10), pack-reused 0
Receiving objects: 100% (4139/4139), 1.30 MiB | 1.36 MiB/s, done.
Resolving deltas: 100% (25/25), done.
Tapped 1 command and 4041 casks (4,148 files, 4.1MB).
==> Satisfying dependencies
==> Downloading https://github.com/ryanoasis/nerd-fonts/releases/download/v2.0.0
==> Downloading from https://github-production-release-asset-2e65be.s3.amazonaws
######################################################################## 100.0%
==> Verifying SHA-256 checksum for Cask 'font-sourcecodepro-nerd-font'.
==> Installing Cask font-sourcecodepro-nerd-font
==> Moving Font 'Sauce Code Pro Light Italic Nerd Font Complete.ttf' to '/Users/
==> Moving Font 'Sauce Code Pro Bold Italic Nerd Font Complete.ttf' to '/Users/j
==> Moving Font 'Sauce Code Pro Medium Nerd Font Complete.ttf' to '/Users/jerry/
==> Moving Font 'Sauce Code Pro Medium Italic Nerd Font Complete.ttf' to '/Users
==> Moving Font 'Sauce Code Pro ExtraLight Nerd Font Complete.ttf' to '/Users/je
==> Moving Font 'Sauce Code Pro ExtraLight Italic Nerd Font Complete.ttf' to '/U
==> Moving Font 'Sauce Code Pro Black Nerd Font Complete.ttf' to '/Users/jerry/L
==> Moving Font 'Sauce Code Pro Black Italic Nerd Font Complete.ttf' to '/Users/
==> Moving Font 'Sauce Code Pro Nerd Font Complete.ttf' to '/Users/jerry/Library
==> Moving Font 'Sauce Code Pro Semibold Nerd Font Complete.ttf' to '/Users/jerr
==> Moving Font 'Sauce Code Pro Semibold Italic Nerd Font Complete.ttf' to '/Use
==> Moving Font 'Sauce Code Pro Light Nerd Font Complete.ttf' to '/Users/jerry/L
==> Moving Font 'Sauce Code Pro Bold Nerd Font Complete.ttf' to '/Users/jerry/Li
🍺 font-sourcecodepro-nerd-font was successfully installed!

iTerm2 設定

1
Preferences > Profiles > Text > Change Font

選擇 SauceCodePro Nerd Font

查詢其他 nerd 字型,

1
$ brew cask search nerd

安裝 zsh

1
2
3
4
5
6
$ brew install zsh

==> Downloading https://homebrew.bintray.com/bottles/zsh-5.6.2.high_sierra.bottl
######################################################################## 100.0%
==> Pouring zsh-5.6.2.high_sierra.bottle.tar.gz
🍺 /usr/local/Cellar/zsh/5.6.2: 1,478 files, 12.8MB

設為預設的 shell

1
2
$ sudo sh -c "echo $(which zsh) >> /etc/shells"
$ chsh -s $(which zsh)

安裝 oh-my-zsh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Cloning Oh My Zsh...
Cloning into '/Users/jerry/.oh-my-zsh'...
remote: Counting objects: 898, done.
remote: Compressing objects: 100% (754/754), done.
remote: Total 898 (delta 25), reused 732 (delta 17), pack-reused 0
Receiving objects: 100% (898/898), 595.72 KiB | 755.00 KiB/s, done.
Resolving deltas: 100% (25/25), done.
Looking for an existing zsh config...
Found ~/.zshrc. Backing up to ~/.zshrc.pre-oh-my-zsh
Using the Oh My Zsh template file and adding it to ~/.zshrc
__ __
____ / /_ ____ ___ __ __ ____ _____/ /_
/ __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \
/ /_/ / / / / / / / / / / /_/ / / /_(__ ) / / /
\____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/
/____/ ....is now installed!


Please look over the ~/.zshrc file to select plugins, themes, and options.

p.s. Follow us at https://twitter.com/ohmyzsh.

p.p.s. Get stickers and t-shirts at https://shop.planetargon.com.

安裝完成後會建立資料夾 ~/.oh-my-zsh

套用主題

1. 開啟 .zshrc

1
$ vim ~/.zshrc

2. 套用主題

預設風格為 ZSH_THEME="robbyrussell",修改如下,

1
2
3
4
## 使用agnoster主題預設為robbyrussell
ZSH_THEME="agnoster"
## 隱藏用戶名稱(user@hostname)
DEFAULT_USER=`id -un`

3. 立即套用

1
$ exec $SHELL

安裝 powerlevel9k 主題

1. 下載 powerlevel9k

1
2
3
4
5
6
7
8
9
$ git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k

Cloning into '/Users/jerry/.oh-my-zsh/custom/themes/powerlevel9k'...
remote: Enumerating objects: 29, done.
remote: Counting objects: 100% (29/29), done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 6963 (delta 7), reused 16 (delta 6), pack-reused 6934
Receiving objects: 100% (6963/6963), 1.96 MiB | 881.00 KiB/s, done.
Resolving deltas: 100% (4447/4447), done.

2. 套用

修改 ~/.zshrc

1
2
3
4
5
6
7
8
9
10
11
# 使用powerlevel9k主題亦可選擇使用agnoster或預設robbyrussell
ZSH_THEME="powerlevel9k/powerlevel9k"
# 隱藏用戶名稱(user@hostname)
DEFAULT_USER=`id -un`
# 含有icon的字型
POWERLEVEL9K_MODE='nerdfont-complete'
# command line 左邊想顯示的內容(資料夾路徑、資料夾讀寫狀態、版本控制資訊)
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir dir_writable vcs) # <= left prompt 設了 "dir"
# command line 右邊想顯示的內容(狀態、RAM、CPU、時間)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status ram load time)
POWERLEVEL9K_PROMPT_ON_NEWLINE=true

其他顯示指令可參考 Available Prompt Segments

Plugins

安裝 zsh-completions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$ brew install zsh-completions

Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core, homebrew/cask).
==> Updated Formulae
ark bullet hapi-fhir-cli nano prettier rustup-init vips
bind convox libphonenumber ntopng qt skaffold xcodegen

==> Downloading https://github.com/zsh-users/zsh-completions/archive/0.28.0.tar.gz
==> Downloading from https://codeload.github.com/zsh-users/zsh-completions/tar.gz/0.28.0
######################################################################## 100.0%
==> Caveats
To activate these completions, add the following to your .zshrc:

fpath=(/usr/local/share/zsh-completions $fpath)

You may also need to force rebuild `zcompdump`:

rm -f ~/.zcompdump; compinit

Additionally, if you receive "zsh compinit: insecure directories" warnings when attempting
to load these completions, you may need to run this:

chmod go-w '/usr/local/share'
==> Summary
🍺 /usr/local/Cellar/zsh-completions/0.28.0: 145 files, 1MB, built in 9 seconds

修改 ~/.zshrc

1
2
3
4
5
6
...
ZSH=$HOME/.oh-my-zsh

# zsh-completions
fpath=(/usr/local/share/zsh-completions $fpath)

Rebuild .zcompdump

1
2
$ rm -f ~/.zcompdump; compinit

安裝 zsh-autosuggestions

1
2
3
4
5
6
7
8
$ git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

Cloning into '/Users/jerry/.oh-my-zsh/custom/plugins/zsh-autosuggestions'...
remote: Counting objects: 1783, done.
remote: Total 1783 (delta 0), reused 0 (delta 0), pack-reused 1783
Receiving objects: 100% (1783/1783), 394.96 KiB | 267.00 KiB/s, done.
Resolving deltas: 100% (1126/1126), done.

zsh-users/zsh-autosuggestions - Fish-like autosuggestions for zsh

安裝 autojump

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ brew install autojump

Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core, homebrew/cask).
==> Updated Formulae
git-archive-all

==> Downloading https://homebrew.bintray.com/bottles/autojump-22.5.1.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring autojump-22.5.1.high_sierra.bottle.tar.gz
==> Caveats
Add the following line to your ~/.bash_profile or ~/.zshrc file (and remember
to source the file to update your current session):
[ -f /usr/local/etc/profile.d/autojump.sh ] && . /usr/local/etc/profile.d/autojump.sh

If you use the Fish shell then add the following line to your ~/.config/fish/config.fish:
[ -f /usr/local/share/autojump/autojump.fish ]; and source /usr/local/share/autojump/autojump.fish

zsh completions have been installed to:
/usr/local/share/zsh/site-functions
==> Summary
🍺 /usr/local/Cellar/autojump/22.5.1: 20 files, 168.2KB

wting/autojump - A cd command that learns - easily navigate directories from the command line

啟用 plugin

oh-my-zsh 內建的 plugin 都放在 ~/.oh-my-zsh/plugins

1
$ ls ~/.oh-my-zsh/plugins

修改 .zshrc

1
plugins=(git autojump zsh-autosuggestions)

Reference