丝滑高效的zsh配置清单

在 Linux 中绕不开的就是 shell,可选择的 shell 有很多,比如 bash、zsh、fish、tcsh,ksh 等等。其中 bash 在 shell 一直是老大哥的地位,zsh 在 bash 基础上,做出了大量改进,同时加入了Bash、ksh及tcsh的某些功能。

因此作者选择在工作中使用 zsh 作为主力 shell。本文将详细介绍如何通过配置 zsh,提升操作丝滑度,增强使用效率,让你从此对 shell 爱不释手。

zsh 安装

zsh 一般系统会自带,可以通过下面的命令查看是否已经安装过 zsh

1
cat /etc/shells

若显示已经安装了,可跳过本节内容。

安装

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

#将 zsh 设置为系统默认 shell
chsh -s $(which zsh)

更新

1
2
3
4
5
6
# 查看当前版本
zsh --version

# 更新
sudo apt update
sudo apt install --only-upgrade zsh

使用 oh-my-zsh 简化 zsh 配置

特别注意:使用 git 时,需要将 git config --global core.autocrlf true重置为 git config --global core.autocrlf input,否则插件会提示错误。

开启默认插件 Z

oh-my-zsh 自带了 z 插件,可以使用 z 自动快速跳转目录,只需要在配置中打开即可

1
2
3
4
5
6
vim ~/.zshrc
# 打开 ~/.zshrc 向里面 plugins 中注册插件
plugins=(
# other plugins...
z
)

自动补全 zsh-autosuggestions

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

vim ~/.zshrc
# 打开 ~/.zshrc 向里面 plugins 中注册插件
plugins=(
# other plugins...
zsh-autosuggestions
)

参考:zsh-autosuggestions/INSTALL.md at master · zsh-users/zsh-autosuggestions (github.com)

语法高亮 fast-syntax-highlighting

1
2
3
4
5
6
7
8
9
git clone https://github.com/zdharma-continuum/fast-syntax-highlighting.git \
${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/fast-syntax-highlighting

vim ~/.zshrc
# 打开 ~/.zshrc 向里面 plugins 中注册插件
plugins=(
# other plugins...
fast-syntax-highlighting
)

也可以使用 zsh-syntax-highlighting

参考:zdharma-continuum/fast-syntax-highlighting: Feature-rich syntax highlighting for ZSH (github.com)

自动完成 zsh-autocomplete

1
2
3
4
5
6
# 下载插件,放到 .oh-my-zsh 插件目录中,是为了方便管理
git clone --depth 1 -- https://github.com/marlonrichert/zsh-autocomplete.git ${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-autocomplete

vim ~/.zshrc
# 在任何调用 compdef 之前添加如下内容 (!!! 不要在 plugins 中直接添加插件)
source ~/.oh-my-zsh/custom/plugins/zsh-autocomplete/zsh-autocomplete.plugin.zsh

这个插件可以搜索历史记录,用起来起飞~

应用配置

1
source ~/.zshrc

未完待续...

参考

本文参考以下文章,在此致以诚挚谢意!

5 Most Frequently Used Open Source Shells for Linux (tecmint.com)

Zsh

ohmyzsh

我是如何让我的 Zsh 像丝般顺滑的 | QuarticCat's Blog

dotfiles/zsh at main · QuarticCat/dotfiles (github.com)