zInit插件安裝配置

安裝

自動安裝

官方推薦的安裝方式,一鍵完成。不過讓我很沒有安全感,我傾向於手動安裝。

sh -c "$(curl -fsSL https://raw.githubusercontent.com/zdharma/zinit/master/doc/install.sh)" 

手動安裝

首先 clone repo 到隨便哪個位置

git clone https://github.com/zdharma/zinit.git ~/.zinit/bin

然後在你的 ~/.zshrc 頂端添加如下語句

source ~/.zinit/bin/zinit.zsh

安裝完成,非常簡單。

配置

從 GitHub repo 加載插件

最爲常見的加載方式,和其他插件管理器的用法是一致的,直接 zinit light {用戶名}/{repo名} 即可。

zinit light zsh-users/zsh-autosuggestions
zinit light zdharma/fast-syntax-highlighting
# PS. zinit 和 fast-syntax-highlighting 是同一個作者,這位作者對速度的追求確實讓人欽佩

加載 oh my zsh 插件

OMZ 別的不說,插件是真的多,如果不能加載 OMZ 插件那就太遺憾了。因此像 antigen 這樣的插件管理器甚至直接提供了整合 OMZ 框架的能力。

zinit 並沒有直接整合 OMZ,不過提供了更爲強大的 snippet 命令來加載單獨的插件。

單文件插件

snippet 命令可以接 URI 來直接加載單個文件。比如加載 OMZ 的 sudo 插件:

zinit snippet OMZ::plugins/sudo/sudo.plugin.zsh

後面那部分看起來好像不像一個 URI?因爲 OMZ 實在太常用了,因此可以用 OMZ:: 來代替它的 repo 地址(對於 Prezto 則是 PZT::),上面的寫法其實等價於:

zinit snippet https://github.com/ohmyzsh/ohmyzsh/blob/master/plugins/sudo/sudo.plugin.zsh

多文件插件

有時候,插件目錄裏不止一個文件(比如包含插件+補全),這時就需要使用 svn 修飾詞來加載了:

zinit ice svn
zinit snippet OMZ::plugins/extract

這裏不詳細解釋什麼叫修飾詞了,你可以理解成一種實現可選參數的手段,修飾詞只對接下來的一條命令起效。 格式是 zinit ice {修飾詞1} {修飾詞2} ...

svn 修飾詞,表示下一行的 URI 需要使用 SVN 協議加載。此時 zinit 會使用 SVN 協議下載整個目錄,自動識別並加載需要的文件。

加載補全

補全文件,有兩種加載方式:1. 使用 svn 修飾詞直接加載目錄,zinit 會自動識別並加載補全。 2. 直接加載補全文件,此時需要使用 as="completion" 這個修飾詞,它會讓 zinit 將下一行命令加載的文件作爲補全安裝到指定目錄:

zinit ice as="completion"
zinit snippet OMZ::plugins/cargo/_cargo

加載 OMZ 插件時需要注意的點

OMZ 的部分插件/主題會依賴 OMZ 本身提供的功能。比如 git 插件,如果想要正常使用的話,需要加載 OMZ 的 git 庫。

# 加載 OMZ 的 git 庫
zinit snippet OMZ::lib/git.zsh
# 加載 OMZ 的 git 插件
zinit snippet OMZ::plugins/git/git.plugin.zsh

也就是說,雖然 zinit 並沒有提供直接加載 OMZ 框架的能力,但是你可以使用 snippet 功能選擇性加載框架的部分功能。這比其他插件管理器的做法更爲靈活。

比如我的配置裏就直接加載了 OMZ 的部分庫,如果你也是 OMZ 的用戶,建議同樣加載這些庫,能保證體驗一致。(比如鍵位綁定)

zinit snippet OMZ::lib/clipboard.zsh
zinit snippet OMZ::lib/completion.zsh
zinit snippet OMZ::lib/history.zsh
zinit snippet OMZ::lib/key-bindings.zsh
zinit snippet OMZ::lib/git.zsh
zinit snippet OMZ::lib/theme-and-appearance.zsh

Turbo Mode

終於說到 zinit 的最強 feature 了。沒有 Turbo Mode 的 zinit 不過是略爲優秀的插件管理器。然而有了 Turbo Mode,zinit 可以說是頂級 zsh 插件管理器,沒有之一!

這個功能非常強大,基本用法卻很簡單。舉例來說,下面的代碼,就實現了 OMZ git 插件的延遲加載。

zinit ice lucid wait='0'
zinit snippet OMZ::plugins/git/git.plugin.zsh

延遲加載用到了兩個修飾詞:lucid,用於靜默加載。wait={秒},在 prompt 加載完畢後的若干秒後再加載。上面的例子就是讓 prompt 啓動後立刻加載 git 插件。

如果堅持追求極致速度的話,可以參考:zplugin_tutorial/#turbo-mode-加載複雜的命令提示符

安裝一些下面用到的插件

安裝lua

yum -y install libtermcap-devel gc ncurses-devel libevent-devel readline-devel gc lua.x86_64

安裝unzip

yum install -y unzip.x86_64

安裝svn

yum install -y subversion

安裝exa

安裝make

dnf group install "Development Tools"
dnf -y install glibc.x86_64
yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel make.x86_64 cmake.x86_64 autoconf.noarch

安裝exa

# 下載對應二進制版本並解壓安裝到指定目錄
cd /usr/local/src
wget https://github.com/ogham/exa/releases/download/v0.9.0/exa-linux-x86_64-0.9.0.zip
unzip exa-linux-x86_64-0.9.0.zip
mv exa-linux-x86_64 /usr/local/bin/exa

# 下載並安裝對應的 MAN 手冊
wget https://raw.githubusercontent.com/ogham/exa/master/contrib/man/exa.1
mv exa.1 /usr/share/man/man1

zinit常用命令:

# 加載插件
$ zinit load {plg-spec}

# 加載插件,不打印加載信息
$ zinit light [-b] {plg-spec}

# 加載單文件,-f 表示不使用緩存,即強制重新下載
$ zinit snippet [-f] {url}

# 卸載插件,-q 表示 quiet
$ zinit unload [-q] {plg-spec}

# 顯示插件加載時間,以插件加載順序排序。-s 表示以秒進行顯示(默認毫秒),-m 表示顯示插件加載時刻
$ zinit times [-s] [-m]

# 顯示 Zinit 狀態
$ zinit zstatus

# 顯示插件加載狀態,--all 表示顯示所有插件
$ zinit report {plg-spec} | --all

# 顯示已加載的插件(使用關鍵字進行過濾)
$ zinit loaded [keyword],list [keyword]

# 顯示每個插件的設置的按鍵綁定
$ zinit bindkeys

# 編譯插件
$ zinit compile {plg-spec} | --all

# 移除已編譯的插件
$ zinit uncompile {plg-spec} | --all

# 顯示已編譯的插件
$ zinit compiled

# 更新 Zinit
$ zinit self-update

# 更新插件/腳本,--all 表示更新所有插件/腳本,-q 表示靜默更新,-r | --reset 更新前執行 git reset --hard / svn revert
$ zinit update [-q] [-r] {plg-spec} | URL | --all

# 爲下一條 zinit 命令添加 ice 描述符
$ zinit ice <ice specification>

# 磁盤刪除插件/腳本,--all 表示清除,--clean 表示刪除未加載的插件/腳本
$ zinit delete {plg-spec} | URL | --clean | --all

# 進入插件目錄
$ zinit cd {plg-spec}

zinit 管理

  • zinit 升級
zinit self-update
  • 升級其他插件
zinit update
  • 清理沒有加載的插件
zinit delete --clean

我的zsh配置

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
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

# 歷史命令日期顯示格式
HIST_STAMPS="yyyy-mm-dd"

# 補全快捷鍵重設
bindkey ',' autosuggest-accept

# source your own zsh file if exists
[ -f ~/.zsh.sh ] && source ~/.zsh.sh

# source your own shrc file if exists
[ -f ~/.env.sh ] && source ~/.env.sh

# source your own alias file if exists
[ -f ~/.alias.sh ] && source ~/.alias.sh

# source your own function file if exists
[ -f ~/.fun.sh ] && source ~/.fun.sh

# source your proxy shrc file if exists
[ -f ~/.proxy.sh ] && source ~/.proxy.sh

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh


# 下面都是zinit配置
source ~/.zinit/bin/zinit.zsh

# 加載 powerlevel10k 主題
zinit ice depth=1; zinit light romkatv/powerlevel10k

# fzf文本增強工具
zinit ice from"gh-r" as"program"
zinit load junegunn/fzf-bin
zinit light Aloxaf/fzf-tab

# 快速目錄跳轉
zinit ice lucid wait='1'
zinit light skywind3000/z.lua

zinit light zsh-users/zsh-completions
zinit light zsh-users/zsh-autosuggestions
zinit light zdharma/fast-syntax-highlighting
# zinit light zsh-users/zsh-syntax-highlighting

# 加載 OMZ 框架及部分插件
zinit snippet OMZ::lib/clipboard.zsh
zinit snippet OMZ::lib/completion.zsh
zinit snippet OMZ::lib/history.zsh
zinit snippet OMZ::lib/key-bindings.zsh
zinit snippet OMZ::lib/git.zsh
zinit snippet OMZ::lib/theme-and-appearance.zsh
zinit snippet OMZ::plugins/colored-man-pages/colored-man-pages.plugin.zsh
zinit snippet OMZ::plugins/sudo/sudo.plugin.zsh

zinit ice svn
zinit snippet OMZ::plugins/extract
zinit ice lucid wait='1'
zinit snippet OMZ::plugins/git/git.plugin.zsh


# ---- (可選)加載了一堆二進制程序 ----
zinit light zinit-zsh/z-a-bin-gem-node

zinit as="null" wait="1" lucid from="gh-r" for \
    mv="exa* -> exa" sbin       ogham/exa \
    mv="*/rg -> rg"  sbin		BurntSushi/ripgrep \
    mv="fd* -> fd"   sbin="fd/fd"  @sharkdp/fd \
    sbin="fzf"       junegunn/fzf-bin

# 加載它們的補全等
zinit ice mv="*.zsh -> _fzf" as="completion"
zinit snippet 'https://github.com/junegunn/fzf/blob/master/shell/completion.zsh'
zinit snippet 'https://github.com/junegunn/fzf/blob/master/shell/key-bindings.zsh'
zinit ice as="completion"
zinit snippet 'https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/fd/_fd'
zinit ice mv="*.zsh -> _exa" as="completion"
zinit snippet 'https://github.com/ogham/exa/tree/master/completions/completions.zsh'

# 不需要花裏胡哨的 ls,我們有更花裏胡哨的 exa
DISABLE_LS_COLORS=true
alias ls=exa
# 配置 fzf 使用 fd
export FZF_DEFAULT_COMMAND='fd --type f'
# ---- 加載完了 ----

參考:

zinit

fzf-tab

我就感覺到快 —— zsh 和 oh my zsh 冷啓動速度優化

加速你的 zsh —— 最強 zsh 插件管理器 zplugin/zinit 教程

zsh 啓動速度慢的終極解決方案

Quit using nvm:快刪掉這個佔據 Zsh 啓動時間一半的怪物!

使用 zinit 管理 zsh 插件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章