目录

Zsh 安装配置

Arch Linux 主流选择有 Fish 和 Zsh, Fish 开箱即用但是不兼容 Bash, 配置为系统默认 Shell 会有很多问题, Zsh 兼容 Bash, 可以作为系统默认 Shell, 但是配置比较复杂并且启动速度不如 Fish.

首先安装 Zsh 本体:

yay -S zsh

oh-my-zsh 项目是 Zsh 配置的主流选择, 但是安装之后无插件启动 Shell 启动速度也会很慢, 大概会到 200ms 左右, 启用一些插件之后会更慢, 所以我们选择自行安装插件管理器 Antidote 来配置插件:

yay -S zsh-antidote

Antidote 可以对插件进行声明式配置, 首先为 .zshrc 加入 Antidote 的启动配置:

zsh_plugins=${ZDOTDIR:-~}/.zsh_plugins
[[ -f ${zsh_plugins}.txt ]] || touch ${zsh_plugins}.txt
fpath=(/usr/share/zsh-antidote/functions $fpath)
autoload -Uz antidote
if [[ ! ${zsh_plugins}.zsh -nt ${zsh_plugins}.txt ]]; then
  antidote bundle <${zsh_plugins}.txt >|${zsh_plugins}.zsh
fi
source ${zsh_plugins}.zsh

这是官网推荐的高性能启动方式.

之后配置插件列表, 在 ~/.zsh_plugins.txt 下添加内容:

# Zsh 补全系统
mattmc3/ez-compinit                            # 加载 Zsh 补全系统
zsh-users/zsh-completions kind:fpath path:src  # 增强命令补全

# zephyr 套件补全 zsh 的默认配置
mattmc3/zephyr path:plugins/color       # 色彩相关
mattmc3/zephyr path:plugins/directory   # 目录操作
mattmc3/zephyr path:plugins/editor      # 编辑操作
mattmc3/zephyr path:plugins/environment # 环境变量相关
mattmc3/zephyr path:plugins/history     # 命令历史
mattmc3/zephyr path:plugins/utility     # 工具

# 集成 fzf 和 zoxide
ajeetdsouza/zoxide
Aloxaf/fzf-tab

# P10k 主题
romkatv/powerlevel10k

# 语法高亮和自动提示
zsh-users/zsh-autosuggestions                         # 自动提示
zsh-users/zsh-history-substring-search                # 历史搜索
zdharma-continuum/fast-syntax-highlighting kind:defer # 语法高亮 (延迟加载)

其中 fzf-tab 和 zoxide 插件依赖外部程序, 需要下载:

yay -S eza fzf zoxide

如果不想依赖外部二进制程序, 可以改用纯 zsh 版本的 zsh-z 插件替代 zoxide.

然后打开配置文件 .zshrc 目录, 添加插件配置项:

# P10k 即时提示
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# ez-compinit配置: 开启补全系统缓存
# 路径优先级 $ZSH_COMPDUMP > $XDG_CACHE_HOME/zsh/zcompdump > ~/.cache/zsh/zcompdump
zstyle ':plugin:ez-compinit' 'use-cache' 'yes'

# ez-compinit配置: 使用oh-my-zsh 风格的补全
# 官方提供 styles: gremlin, ohmy, prez, zshzoo
# 只有 ohmy 预设与 fzf-tab 兼容, 其它模式 group 标题会有无法渲染的占位符
zstyle ':plugin:ez-compinit' 'compstyle' 'ohmy'

# zephyr配置: 启用和关闭一些 editor 预设功能
zstyle ':zephyr:plugin:editor' 'prepend-sudo' yes # 快捷键加 sudo 功能
zstyle ':zephyr:plugin:editor' 'pound-toggle' no  # 空格后显示别名原始形态
zstyle ':zephyr:plugin:editor' 'magic-enter' no   # 回车自动执行 ls 或 git 命令

# 配合 fzf-tab 相关补全选项
zstyle ':completion:*:git-checkout:*' sort false   # git-checkout 关闭按字母排序
zstyle ':completion:*:descriptions' format '[%d]'  # 窗口增加 group 标题

# fzf-tab 内部配置
zstyle ':fzf-tab:*' fzf-flags --bind=tab:accept                               # 使用 Tab 键确认 fzf-tab 中的选项
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'eza -1 --color=always $realpath' # 在 cd 界面使用 eza 显示目录内容

# --- Antidote 启动部分 ---
# 官方推荐的高性能启动方式
zsh_plugins=${ZDOTDIR:-~}/.zsh_plugins
[[ -f ${zsh_plugins}.txt ]] || touch ${zsh_plugins}.txt
fpath=(/usr/share/zsh-antidote/functions $fpath)
autoload -Uz antidote
if [[ ! ${zsh_plugins}.zsh -nt ${zsh_plugins}.txt ]]; then
  antidote bundle <${zsh_plugins}.txt >|${zsh_plugins}.zsh
fi
source ${zsh_plugins}.zsh
# --- Antidote 启动结束 ---

# 启动 P10k 主题
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

# 启动fzf-tab
enable-fzf-tab

# 修复 zephyr-directory 预设与补全系统的问题
# 防止操作目录命令的 Tab 选项中出现隐藏文件
unsetopt glob_dots

# 去掉 zephyr-editor 修改光标样式功能并恢复终端默认形态
function update-cursor-style { : }
printf '\e[0 q'

# --- 快捷键绑定 ---
# 绑定 Alt + S 为命令前增加 sudo
bindkey '\es' prepend-sudo

# 配置 zsh-history-substring-search 快捷键
bindkey '^[[A'   history-substring-search-up    2>/dev/null # '^[[A' 序列的上方向键
bindkey '^[OA'   history-substring-search-up    2>/dev/null # '^[[A' 序列的上方向键

bindkey '^[[B'   history-substring-search-down  2>/dev/null # '^[[B' 序列的下方向键
bindkey '^[OB'   history-substring-search-down  2>/dev/null # '^[OB' 序列的下方向键

其中顶部和底部 P10k 主题的配置稍后启动时 P10k 会自动添加. 也可以手动用命令触发 p10k configure

eza 可以代替 ls , fzf 是一个快速的搜索软件, zoxide 可以根据模糊名称智能跳转目录.

之后进入 Zsh 终端

zsh

启动时 Antidote 会从 GitHub 下载插件, 等插件加载好, 第一次进入会有 P10k 的主题选项, 选择自己喜欢的主题配置即可进入.

进入 Shell 之后, 可以进一步加快速度, 由于 fzf-tab 插件需要渲染颜色, 如果渲染量很大, 使用内部 Zsh 脚本渲染会很慢, 所以可以编译 C 版本的渲染模块加速 fzf-tab 界面, 执行以下命令:

build-fzf-tab-module

之后重启 Zsh 即可看到渲染大量颜色时 fzf-tab 速度会有明显提升.

所有插件配置完成可以测试对比一下 Fish 和 Zsh 的速度:

> for i in {1..10}; do time zsh -lic exit; done
zsh -lic exit  0.04s user 0.02s system 108% cpu 0.057 total
zsh -lic exit  0.05s user 0.04s system 113% cpu 0.080 total
zsh -lic exit  0.06s user 0.05s system 112% cpu 0.092 total
zsh -lic exit  0.06s user 0.05s system 116% cpu 0.097 total
zsh -lic exit  0.06s user 0.05s system 115% cpu 0.088 total
zsh -lic exit  0.07s user 0.05s system 116% cpu 0.102 total
zsh -lic exit  0.06s user 0.05s system 115% cpu 0.095 total
zsh -lic exit  0.06s user 0.05s system 116% cpu 0.090 total
zsh -lic exit  0.06s user 0.05s system 116% cpu 0.097 total
zsh -lic exit  0.05s user 0.05s system 113% cpu 0.089 total
> for i in {1..10}; do time fish -lic exit; done
fish -lic exit  0.01s user 0.01s system 102% cpu 0.012 total
fish -lic exit  0.01s user 0.01s system 104% cpu 0.016 total
fish -lic exit  0.00s user 0.00s system 105% cpu 0.009 total
fish -lic exit  0.01s user 0.00s system 101% cpu 0.015 total
fish -lic exit  0.02s user 0.01s system 103% cpu 0.026 total
fish -lic exit  0.01s user 0.01s system 102% cpu 0.012 total
fish -lic exit  0.02s user 0.00s system 105% cpu 0.021 total
fish -lic exit  0.02s user 0.01s system 103% cpu 0.028 total
fish -lic exit  0.01s user 0.01s system 101% cpu 0.024 total
fish -lic exit  0.02s user 0.01s system 104% cpu 0.025 total

可以看到速度还是不错的, 虽然比不上 Fish 的速度, 但都在 100ms 以下.