zsh 插件管理器 - Zinit

2020-04-21  本文已影响0人  Whyn

[TOC]

前言

Zinit 是一个灵活快速的 zsh 插件管理器,它可以自动安装 Github 或者其他网站上的 zsh 插件。

特性

安装

Zinit 提供了自动安装和手动安装两种方法:

核心功能

Zinit 的涉及到的内容很多,完全了解 Zinit 提供的功能其实没有必要,我们只需理解 Zinit 最核心的一些功能点即可。

以下是本人认为的 Zinit 的核心功能,掌握下列内容,应当就可以基本使用 Zinit

ice描述符的执行顺序为:atinit -> atpull! -> make'!!' -> mv -> cp -> make! -> atclone/atpull -> make -> (plugin script loading) -> src -> multisrc -> atload'

其他

以下是 Zinit 提供的另外一些有用的特性以及一些本人觉得好用的操作:

zinit snippet 'https://github.com/robbyrussell/oh-my-zsh/raw/master/plugins/git/git.plugin.zsh'
zinit snippet 'https://github.com/sorin-ionescu/prezto/blob/master/modules/helper/init.zsh'

snippet表示通过curlwget下载的单文件脚本。
对于 Oh-My-Zsh 和 Prezto,Zinit 提供了相应的简写方式:

# OMZ:: 表示 Oh My Zsh 插件
zinit snippet OMZ::plugins/git/git.plugin.zsh
# PZT:: 表示 Prezto 插件
zinit snippet PZT::modules/helper/init.zsh
# URL points to directory
zinit ice svn
zinit snippet PZT::modules/docker
# Load a few important annexes, without Turbo
# (this is currently required for annexes)
zinit light-mode for           \
    zinit-zsh/z-a-patch-dl     \
    zinit-zsh/z-a-as-monitor   \
    zinit-zsh/z-a-bin-gem-node

上述命令就安装了 3 个比较重要的附件:z-a-patch-dlz-a-as-monitorz-a-bin-gen-node

zinit as"null" wait"3" lucid for              \
    sbin"recall"  Fakerr/git-recall           \
    sbin  paulirish/git-open                  \
    sbin  paulirish/git-recent                \
    sbin  davidosomething/git-my              \
    make"PREFIX=$ZPFX install"  iwata/git-now \
    make"PREFIX=$ZPFX"          tj/git-extras

上述单一一条命令就安装了 6 个插件,并且所有插件共享基础ice选项:as"nullwait"3"lucid,可以看到,For 语法确实简洁许多。

sbin描述符是由附件 z-a-bin-gem-node 提供的,sbin选项的作用在于无需更改$PATH就可以在命令行中直接使用其指定的命令。如果命令与插件名称相同,可省略不写。

zinit ice mv"httpstat.sh -> httpstat" \
pick"httpstat" as"program"
zinit snippet \
https://github.com/b4b4r07/httpstat/blob/master/httpstat.sh
zinit ice as"program" atclone"rm -f src/auto/config.cache; ./configure" \
    atpull"%atclone" make pick"src/vim"
zinit light vim/vim

上述命令中的各个参数含义如下:

如果需要执行的是make install操作,则只需将make修改为make"all install"

如果编译(make)完成后,需要进行source操作,则使用src选项,比如:

zinit ice as"program" make'!' atclone'./direnv hook zsh > zhook.zsh' \
    atpull'%atclone' src"zhook.zsh"
zinit light direnv/direnv

上述命令在git clonegit pull完成之前会先执行make操作,然后执行./direnv hook zsh > zhook.zsh,生成zhook.zsh目标文件,最后再进行source zhook.zsh操作。这样子就确保了命令./direnv hook zsh只在git clonegit pull时才执行,其他情况不响应,因此 zsh 可以更快启动。

zinit ice as"completion"
zinit snippet https://github.com/docker/cli/blob/master/contrib/completion/zsh/_docker
zinit ice blockf
zinit light zsh-users/zsh-completions

第一条命令中的blockf指令会阻断传统的补全添加方式,Zinit 会使用自己的方式(基于符号链接而不是添加多个目录到$fpath)。
Zinit 会自动安装新下载插件的补全。如果要卸载补全然后再重新安装,方法如下:

zinit cuninstall zsh-users/zsh-completions   # uninstall
zinit creinstall zsh-users/zsh-completions   # install
zi clist

默认情况下,上述命令每行会显示插件的 3 个补全,也可以通过为clist添加参数指定补全显示个数,比如:

zi clist 6 # 每行显示 6 个补全
$ zi cdisable cmake
Disabled cmake completion belonging to zsh-users/zsh-completions
$ zi cenable cmake
Enabled cmake completion belonging to zsh-users/zsh-completions
zinit csearch
zinit ice svn
zinit snippet https://github.com/zsh-users/zsh-completions/trunk/src

# For Oh My Zsh and Prezto, the OMZ:: and PZT:: prefixes work
# without the need to add the `/trunk/` infix (however the path
# should point to a directory, not to a file):
zinit ice svn; zinit snippet PZT::modules/docker

snippet 也会像插件下载一样,自动安装其补全。

参考

上一篇 下一篇

猜你喜欢

热点阅读