Back
Featured image of post ZSH + ZI + PURE 配置教程

ZSH + ZI + PURE 配置教程

记录一次 ZSH 重配置,更方便的插件管理

最开始用 zsh 的时候,搜到的一堆文章肯定会叫你用 Oh My Zsh,但 OMZ 的默认配置实在令人难以吐槽,而且插件管理很麻烦(和手动 clone 安装没区别)。

这些问题你一定遇到过:

  • 进大项目 git 文件夹输命令卡死(会不断读取文件变化)
  • 粘贴代码卡半天(自动转义)

插件管理安装及介绍

在 Google 搜 zsh plugin manager 出来的都是一堆两三年没更新或者已经 Archive 的 repo(比如 antigen antibody)。

然后在无意之中看到这篇文章:加速你的 zsh —— 最强 zsh 插件管理器 zplugin/zinit 教程

ZI 更名了几次,\(\text{ZPlugin} \rightarrow \text{ZInit} \rightarrow \text{ZI}\)

ZI 相比其他包管理确实方便了不少。不用反复 clone 然后手动添加。只需要简单一行(或者两行)命令,一气呵成。

如果之前装了 zsh 先备份原来的 .zshrc,方便后面参考。

mv ~/.zshrc ~/.zshrc.bak

如果装了 Oh My Zsh,输入 uninstall_oh_my_zsh 卸载。

安装 brew,后面可能会用到,详情可见文档

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

运行官方脚本安装 ZI,其他选项可见官方文档,然后重启终端即可。

sh -c "$(curl -fsSL https://git.io/ge)" --

load / light / snippet

ZI 的命令语法很简单:

zi load [repo-url] # 加载插件
zi light [repo-url] # 快速加载插件
zi snippet [file-url] # 加载代码片段

比如

zi load z-shell/H-S-MW
zi light zsh-users/zsh-syntax-highlighting
zi snippet OMZP::z

loadlight 命令会默认添加 https://github.com/ 前缀;snippet 命令针对 Oh My Zsh 的插件或库,可以直接 OMZP::XXXOMZL::XXX(详见官方文档)。

ice

另外有一条用于添加参数的命令 zi ice。至于为什么叫 ice ,文档里是这么说的……

ice 命令的工作逻辑类似喝饮料或咖啡,冰是添加物。在 ZI 中,ice 仅作用于下一条命令,也就是「被融化了」,并不会持续很长时间。

The logic is that “ice” is something that’s added, e.g. to a drink or a coffee, and in the ZI sense this means that ice is a modifier added to the next ZI command, and also something that melts, so it doesn’t last long, – and in the ZI use it means that the modifier lasts for only single next ZI command.

一些 ice 谓词

ice 用法类似这样:

zi ice wait lucid atinit='zpcompinit'
zi light zdharma/fast-syntax-highlighting

wait 是延迟加载,实质上也是 wait"0" 的简写,在 .zshrc 加载完毕 0 秒后再加载下条命令。

atinit 是在初始化时执行的操作。

若需要将某个仓库的子文件作为插件加载,可以使用 zi ice svn,如:

# 记得安装 SVN: brew install svn
zi ice svn
# 需要把链接里的 tree/branch-name 替换成 trunk
zi snippet https://github.com/ohmyzsh/ohmyzsh/trunk/plugins/macos

可以通过 as"completion" 谓词加载补全文件,如:

zinit ice as"completion"
zinit snippet https://github.com/docker/cli/blob/master/contrib/completion/zsh/_docker 

详细语法介绍见 Ice Syntax,全表见:Ice Modifiers

ZSH 配置

所有编辑都在 ~/.zshrc 上进行

  • 开启 ZSH 自带的自动补全:
setopt AUTO_PUSHD
  • ls 以及 cd 颜色:
export CLICOLOR=1
export LSCOLORS=ExGxFxdaCxDaDahbadeche
zstyle ':completion:*' list-colors "${(@s.:.)LS_COLORS}"
  • 代理设置(这里以 ClashX 的默认端口为例)
export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890
export SSH_AUTH_SOCK=$HOME/Library/Containers/com.maxgoedjen.Secretive.SecretAgent/Data/socket.ssh
  • GPG TTY(如果安装了 GPG)
export GPG_TTY=$(tty)
  • 语言设置,这里我设成英语,git 之类的本地化很难让人看懂
export LANG=en_US.UTF-8
  • 一些个人 alias…
# 平常不建议直接用 rm 命令, 非常容易误删, 如果想直接使用 rm 请 /bin/rm
# 没有针对跨硬盘优化, 操作其他盘的时候请注意
alias rm='move1(){ /bin/mv -f $@ ~/.trash/; };move1 $@'

# 配合上一条 alias 使用
emptytrash() {
  sudo /bin/rm -rf ~/.trash/*
}

alias grep='grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox}'

安装 pure 主题

zi ice pick"async.zsh" src"pure.zsh"
zi light sindresorhus/pure

可以根据文档额外配置:

PURE_PROMPT_SYMBOL="λ"
https://github.com/sindresorhus/pure

插件安装以及简单介绍

我安装的全部插件
zicompinit # <- https://z-shell.pages.dev/docs/gallery/collection#minimal

zi ice wait lucid atinit='zpcompinit'
zi light zdharma/fast-syntax-highlighting
zi ice wait lucid atload'_zsh_autosuggest_start'
zi light zsh-users/zsh-autosuggestions
zi light agkozak/zsh-z
zi light zsh-users/zsh-completions
# brew install exa
zi light DarrinTisdale/zsh-aliases-exa

zi snippet OMZL::git.zsh
zi snippet OMZP::git
zi snippet OMZP::autojump
# brew install --cask visual-studio-code
# or alias code="/path/to/vsc"
# for example: alias code="/Applications/Visual Studio Code.app/Contents/MacOS/Electron"
zi snippet OMZP::vscode
zi snippet OMZ::lib/key-bindings.zsh
# brew install svn
zi ice svn
zi snippet https://github.com/ohmyzsh/ohmyzsh/trunk/plugins/macos

zi ice pick"async.zsh" src"pure.zsh"
zi light sindresorhus/pure

autoload -Uz compinit; compinit
zinit cdreplay -q

zstyle ':completion:*' list-colors "${(@s.:.)LS_COLORS}"
autoload -Uz compinit
compinit

高亮显示

zi ice wait lucid atinit='zpcompinit'
zi light zdharma/fast-syntax-highlighting

提供高亮显示,类似这样:

https://github.com/z-shell/fast-syntax-highlighting

自动补全

为基础命令提供补全。

zi light zsh-users/zsh-completions

自动建议

根据 ZSH 使用记录提供补全建议

zi ice wait lucid atload'_zsh_autosuggest_start'
zi light zsh-users/zsh-autosuggestions

z 和 autojump

zi light agkozak/zsh-z
zi snippet OMZP::autojump

两个都是用来快速跳转目录的,用法:z/j [目录名]

alias 插件

alias 插件是指提供一系列方便简写的插件

  • git,这个不用多解释,gcl gp gl 等等绝对是程序员用的最频繁的 alias
zi snippet OMZL::git.zsh
zi snippet OMZP::git.zsh
  • exa,exa 是对 ls 的现代替代
# 如果没装可以
# brew install exa
zi light DarrinTisdale/zsh-aliases-exa
说是插件其实也没多少行……想知道用法自行展开阅读注释
# Project:  zsh-aliases-exa
# File:     /zsh-aliases-exa.plugin.zsh
# Created:  2019-04-12 19:07:28
# Author:   Darrin Tisdale
# -----
# Modified: 2019-05-14 23:18:24
# Editor:   Darrin Tisdale

# general use
alias ls='exa'                                                         # ls
alias l='exa -lbF --git'                                               # list, size, type, git
alias ll='exa -lbGF --git'                                             # long list
alias llm='exa -lbGF --git --sort=modified'                            # long list, modified date sort
alias la='exa -lbhHigUmuSa --time-style=long-iso --git --color-scale'  # all list
alias lx='exa -lbhHigUmuSa@ --time-style=long-iso --git --color-scale' # all + extended list

# speciality views
alias lS='exa -1'                                                      # one column, just names
alias lt='exa --tree --level=2'                                        # tree
  • vscode,快速打开 Visual Studio Code
# 如果没装可以 brew install --cask visual-studio code
# 如果装了可以在 .zshrc 里添加: alias code="/path/to/vsc"
# 比如 alias code="/Applications/Visual Studio Code.app/Contents/MacOS/Electron"
# 不然可能提示找不到 Visual Studio Code
zi snippet OMZP::vscode
zi snippet OMZ::lib/key-bindings.zsh

我的 .zshrc 文件已经上传至 Gist,欢迎参照。

理论上无需太大更改,可以直接替换使用。(这也是相比 Oh My Zsh 方便的点)

相关文章推荐

comments powered by Disqus