Make a perfect terminal experience from zero in 3 steps

Step1 – 确认已经在使用 zsh

kamus@Kamus-MBP-2015 ~ % echo $SHELL
/bin/zsh

如果不是,安装zsh并设置成默认shell

brew install zsh
chsh

Step2 – 安装必须的字体

因为我们期望在终端中可以显示丰富的图标,因此需要Nerd Font,可以从这个页面中挑选自己喜欢的字体

https://www.nerdfonts.com/

我个人很喜欢Sauce Code Pro这款。

Sauce Code Pro Nerd Font Complete Mono.ttf

Configure your terminal to use this font: Sauce Code Pro Nerd Font

以iTerm2为例,设置界面如下:

当然也可以选择本文要介绍的zsh4humans以及神级zsh样式powerlevel10k的原作者Roman Perepelitsa推荐的MesloLGS NF。

https://github.com/romkatv/powerlevel10k/blob/master/font.md

Step3 – 安装zsh4humans

https://github.com/romkatv/zsh4humans

if command -v curl >/dev/null 2>&1; then
sh -c “$(curl -fsSL https://raw.githubusercontent.com/romkatv/zsh4humans/v5/install)”
else
sh -c “$(wget -O- https://raw.githubusercontent.com/romkatv/zsh4humans/v5/install)”
fi

z4h不但内置了powerlevel10k的配置,而且自身还实现了很多ohmyzsh的插件功能,因此使用了z4h之后就不再需要单独进行onmyzsh的安装和配置,非常方便。

z4h在安装的同时也会拉取一份最新的ohmyzsh,如果确实需要修改z4h的默认配置而要加载更多ohmyzsh的插件,可以在~/.zshrc中进行修改。参看“z4h load ”部分的例子,可以自行加载需要的ohmyzsh包含的插件。

比如如果你更喜欢用autojump,而不是用z4h内置的fzf来进行历史目录的快速跳转;比如你喜欢sudo插件,双击ESC键就可以在上一条命令的前面加上sudo,可以通过在~/.zshrc文件中增加如下行来加载这些插件。

z4h load ohmyzsh/ohmyzsh/plugins/autojump
z4h load ohmyzsh/ohmyzsh/plugins/sudo

注意:以上的autojump仅仅是for zsh的插件,本体程序autojump还是要通过brew install autojump来安装的。

GitHub – wting/autojump: A cd command that learns – easily navigate directories from the command line

实际上根据z4h作者的说法,有很大用处的插件或者功能都基本上已经内置到z4h里了,比如对于上述的autojump功能,z4h内置了fzf,默认的快捷键是Alt(Option)+r,按一下有惊喜。同时还内置了Shift+左箭头快速跳转到之前的目录;Shift+上箭头快速跳转到父目录等快捷键。这些快捷键的设置在.zshrc中可以找到。

# Define key bindings.
z4h bindkey undo Ctrl+/ Shift+Tab # undo the last command line change
z4h bindkey redo Option+/ # redo the last undone command line change

z4h bindkey z4h-cd-back Shift+Left # cd into the previous directory
z4h bindkey z4h-cd-forward Shift+Right # cd into the next directory
z4h bindkey z4h-cd-up Shift+Up # cd into the parent directory
z4h bindkey z4h-cd-down Shift+Down # cd into a child directory

后续如果需要,可以执行一条命令就更新z4h

❯ z4h update
z4h: fetching z4h.zsh
z4h: updating zsh4humans
z4h: updating zsh-history-substring-search
z4h: updating zsh-autosuggestions
z4h: updating zsh-completions
z4h: updating zsh-syntax-highlighting
z4h: updating terminfo
z4h: updating fzf
z4h: updating fzf binary
z4h: updating tmux
z4h: updating ohmyzsh/ohmyzsh
z4h: updating powerlevel10k
z4h: updating gitstatus binary
z4h: initializing zsh
z4h: update successful
z4h: restarting zsh

2 Comments Add yours

  1. Thanks for sharing this, it is really helpful.

Leave a Reply

Your email address will not be published. Required fields are marked *