はじめに
zshでのプラグイン管理でzinitを利用する方法について紹介していきます。
zinitはできることが多いので、今回はとりあえず使い始めてみることを目標に解説します。
zinitとは
zinit
とは、zshのプラグインマネージャーです。要は、プラグインを管理するツールになります。
zinit
は速いみたいで、oh-my-zshやpreztoのzshフレームワークもサポートしているようです。
zshのプラグインマネージャーは以下のようなものがあります。
スターだけでみるとzplug
が多いですが、zinit
の方がコミット数が多かったり、最近のコミットがあったりします。(2021/10/27時点)
インストール
まずはzinit
をインストールします。
sh -c "$(curl -fsSL https://git.io/zinit-install)"
インストールができると~/.zshrc
に下記の内容が追記されます。
### Added by Zinit's installer │
if [[ ! -f $HOME/.zinit/bin/zinit.zsh ]]; then │
print -P "%F{33}▓▒░ %F{220}Installing %F{33}DHARMA%F{220} Initiative Plugin Manager (%F{33}zdharma-continuum/zinit%F{220})…%f" │
command mkdir -p "$HOME/.zinit" && command chmod g-rwX "$HOME/.zinit" │
command git clone https://github.com/zdharma-continuum/zinit "$HOME/.zinit/bin" && \ │
print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \ │
print -P "%F{160}▓▒░ The clone has failed.%f%b" │
fi │
│
source "$HOME/.zinit/bin/zinit.zsh" │
autoload -Uz _zinit │
(( ${+_comps} )) && _comps[zinit]=_zinit │
### End of Zinit's installer chunk
~/.zshrc
を読み込んで、zinit
をリロードします。
source ~/.zshrc
zinit self-update
これでzinit
を利用する準備は完了です。
プラグインの追加
プラグインの追加には2つの方法があります。
load
:トラッキングありlight
:トラッキングなし(高速)
基本は以下のようにlight
を使えばいいと思います。
zinit light zsh-users/zsh-completions
zinit light zsh-users/zsh-autosuggestions
zinit iceとは?
zinit ice
は、次に続くzinit
コマンドのオプション的なものを指定できます。
以下のように後続のコマンドの動作を指定します。飲み物に入れる「氷」のように追加されるものという意味でice
だそうです。
zinit ice wait lucid
zinit light zsh-users/zsh-completions
wait
で遅延ロードし、lucid
でロードした時にコンソールに情報が出力されるのを省略させています。
ice
でさまざまな挙動を制御できます。
Alternate Ice Syntax - Zinit Wiki
設定紹介
参考程度に現状の私の.zshrc
を記載しておきます。
# ---------------------------------------------------------
# Enable Powerlevel10k
# ---------------------------------------------------------
# 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
# ---------------------------------------------------------
# Zinit's installer
# ---------------------------------------------------------
if [[ ! -f $HOME/.zinit/bin/zinit.zsh ]]; then
print -P "%F{33}▓▒░ %F{220}Installing %F{33}DHARMA%F{220} Initiative Plugin Manager (%F{33}zdharma-continuum/zinit%F{220})…%f"
command mkdir -p "$HOME/.zinit" && command chmod g-rwX "$HOME/.zinit"
command git clone https://github.com/zdharma-continuum/zinit "$HOME/.zinit/bin" && \
print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \
print -P "%F{160}▓▒░ The clone has failed.%f%b"
fi
source "$HOME/.zinit/bin/zinit.zsh"
autoload -Uz _zinit
(( ${+_comps} )) && _comps[zinit]=_zinit
# ---------------------------------------------------------
# plugin list
# ---------------------------------------------------------
zinit ice wait lucid
zinit light zsh-users/zsh-completions # 補完
zinit ice wait lucid
zinit light zsh-users/zsh-autosuggestions # 補完
zinit ice wait lucid
zinit light zsh-users/zsh-syntax-highlighting
zinit ice wait lucid
zinit light chrissicool/zsh-256color
zinit ice depth=1
zinit light romkatv/powerlevel10k
# ---------------------------------------------------------
# basic
# ---------------------------------------------------------
# 履歴保存管理
HISTFILE=$ZDOTDIR/.zsh-history
HISTSIZE=100000
SAVEHIST=1000000
# 他のzshと履歴を共有
setopt inc_append_history
setopt share_history
# ---------------------------------------------------------
# alias
# ---------------------------------------------------------
alias ls='ls -G'
alias python="python3"
alias pip="pip3"
# ---------------------------------------------------------
# prompt
# ---------------------------------------------------------
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
# ---------------------------------------------------------
# completion
# ---------------------------------------------------------
# コマンド補完
autoload -Uz compinit && compinit
# 小文字でも大文字にマッチ
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'
# 補完候補をTabや矢印で選択可能
zstyle ':completion:*:default' menu select=1
# ---------------------------------------------------------
# hight-light
# ---------------------------------------------------------
# サジェストの色変更
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=244"
# lsコマンドで色分けする
zstyle ':completion:*' list-colors $LSCOLORS
# ---------------------------------------------------------
# peco
# ---------------------------------------------------------
# コマンド履歴検索 ctrl+r
function peco-history-selection() {
BUFFER=`history -n 1 | tac | awk '!a[$0]++' | peco`
CURSOR=$#BUFFER
zle reset-prompt
}
zle -N peco-history-selection
bindkey '^R' peco-history-selection
# コマンド履歴からディレクトリ検索・移動 ctrl+e
if [[ -n $(echo ${^fpath}/chpwd_recent_dirs(N)) && -n $(echo ${^fpath}/cdr(N)) ]]; then
autoload -Uz chpwd_recent_dirs cdr add-zsh-hook
add-zsh-hook chpwd chpwd_recent_dirs
zstyle ':completion:*' recent-dirs-insert both
zstyle ':chpwd:*' recent-dirs-default true
zstyle ':chpwd:*' recent-dirs-max 1000
zstyle ':chpwd:*' recent-dirs-file "$HOME/.cache/chpwd-recent-dirs"
fi
function peco-cdr () {
local selected_dir="$(cdr -l | sed 's/^[0-9]* *//' | peco)"
if [ -n "$selected_dir" ]; then
BUFFER="cd ${selected_dir}"
zle accept-line
fi
}
zle -N peco-cdr
bindkey '^E' peco-cdr
# カレントディレクトリ以下のディレクトリ検索・移動 ctrl+x
function find_cd() {
local selected_dir=$(find . -type d | peco)
if [ -n "$selected_dir" ]; then
BUFFER="cd ${selected_dir}"
zle accept-line
fi
}
zle -N find_cd
bindkey '^X' find_cd
まとめ
zinit
でzshプラグインの管理ができる
参考
- zdharma/zinit: Flexible and fast Zsh plugin manager with clean fpath, reports, completion management, Turbo, annexes, services, packages.
- Zinit Wiki
- zdharma-continuum/zinit: 🌻 Flexible and fast ZSH plugin manager
- https://github.com/zdharma has suddenly disappeared. I haven't found any statement from Sebastian as to why. Sebastian Gniazdowski is the author of well know projects such as
zinit
andfast-syntax-highlighting
and regular contributor to this community. Anyone have any background about why? : zsh