人生苦短,我用 zsh !

1 什么是 shell ?

在计算机科学中,shell(外壳)是一种「提供使用者使用界面」的软件,区别于 kernel(内核),和它类似的有 Windows 下的 cmd.exe。简单来说,shell 就是一个「命令解释器」,外壳通过接收用户的命令来调用内核执行。

同时 shell 本身也是一门脚本语言,可以交互式执行用户编写的一系列命令。我们常常会写一些 shell 脚本,做一些自动化的工作。

通常情况下,shell 被分为两大类:

  1. 图形界面 shell (GUI shell)
  2. 命令行式 shell (CLI shell)

Windows Explorer 是一个典型的图形界面 shell,而我们常常说的 shell 一般是指 CLI shell ,在下文中如果无特别说明,shell 就是指 CLI shell。zsh 就是 CLI shell 的一种,除此之外还有 bash / sh / ksh 等,以及 Windows 下的 PowerShell 。

本文使用 Mac OS 作为演示环境,相同的命令一般在 Linux 下也能顺利执行。

打开命令行输入:

1
cat /etc/shells

可以看到当前系统中所有已经安装 shell 类型:

1
2
3
4
5
6
7
8
9
10
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

如果你对 Linux 有一定的了解就会知道,通常情况下 Linux 系统会把 bash 作为默认的系统 shell。bash 的全名为 Bourne Again SHell,Bourne 是某项工作的资助者的名字,以其命名了 Bourne shell。后来 GNU 成员为了 GNU / Linux 项目将先前的 Bourne shell 进行重新完善,就有了新的 Bourne Again Shell。为什么叫 Bourne Again Shell 呢?主要是Bourne Again音同Born Again,有「重生」之意。

2 zsh !!!

既然 bash 作为系统默认 shell,而且有 GNU 这样实力雄厚的开源大哥作为支持,自然也不会太差,那为什么要推荐用 zsh 呢?

主要是 zsh 有以下的优点:

  1. 命令提示
  2. 智能补全
  3. 色彩高亮
  4. 快速跳转
  5. 自动跳转

废话少说,先看东西。

1
2
3
4
5
6
7
8
# 安装
sudo apt install zsh

# 设 zsh 为默认 shell,下次登录时默认启动 zsh
chsh -s /bin/zsh

# 启用 zsh
zsh

- 我现在启动了,为什么没有你说的那么厉害?

- 因为你还没配置好!

使用脚本自动配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cd ~

# 下载配置
wget https://raw.githubusercontent.com/skywind3000/vim/master/etc/zshrc.zsh
# 网络不行的话,试试
wget https://rawgithubusercontent.deno.dev/skywind3000/vim/master/etc/zshrc.zsh

# 拷贝 zshrc.zsh 中的内容到 .zshrc 中
cat zshrc.zsh > .zshrc

# 修改 zsh 主题为 fishy (这一步是可选的)
vi .zshrc
# 去掉前面的 # 注释
antigen theme fishy

# 执行下面命令使其立即生效。
source .zshrc

# 用完删掉
rm zshrc.zsh

上面这部分可能因为网络原因无法成功。

1
2
3
4
# 上面不行试试这个原始的 ohmyzsh
sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"
# 网络不行的话,试试
sh -c "$(wget https://rawgithubusercontent.deno.dev/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"

更换一个非常棒的主题

1
2
3
4
5
6
7
8
cd ~
vi .zshrc

# 修改主题为
ZSH_THEME="agnoster" # (this is one of the fancy ones)
# see https://github.com/ohmyzsh/ohmyzsh/wiki/Themes#agnoster

source .zshrc

接下来你就可以体验利器 zsh 的神奇之处,输入一些命令试试看吧~

3 插件

自动补全命令

1
2
3
4
5
6
7
8
9
git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions

vi ~/.zshrc
# 添加
plugins=(
zsh-autosuggestions
)

source ~/.zshrc

autojump

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
git clone https://github.com/joelthelion/autojump.git $ZSH_CUSTOM/plugins/autojump

cd $ZSH_CUSTOM/plugins/autojump

# 如果系统中没有 python,但是有 python3,可以考虑用软链接
sudo ln -s /usr/bin/python3 /usr/bin/python

python install.py
# 安装后会提示将一些配置加入 .zshrc 中,类似于
[[ -s $HOME/.autojump/etc/profile.d/autojump.sh ]] && source $HOME/.autojump/etc/profile.d/autojump.sh
autoload -U compinit && compinit -u

vi ~/.zshrc
# 添加上述配置
source ~/.zshrc

4 参考

作者

Ailln

发布于

2018-06-13

更新于

2024-03-02

许可协议

评论