🔀 標準の構文
Introduction
Ziは、構造化された文・式に対して2つの構文タイプを提供します。
- 標準構文
- The "For" syntax
どちらの構文を使うかはユーザー次第ですが、両方の構文に慣れることを強くお勧めします。 In this example, we will use an empty repository z-shell/0 to practice the basics of the standard syntax.
- ターミナルで以下のコマンドを実行します。
zi load z-shell/0
Successfully installed the Zsh plugin which usually contains all the setup instructions as described in the Zsh plugin standard.
スニペットは、再利用可能なソースコード、マシンコード、またはテキストの一部を含む単一のファイルで、ファイルへのフルパスまたはURLが必要です。
- ターミナルで以下のコマンドを実行します。
zi snippet https://raw.githubusercontent.com/z-shell/zi-src/main/lib/zsh/snippets/welcome.zsh
成功しました! しかし、必ずしもとても簡単でシンプルであるとは限りません。時には、特定の時間や条件であることを指定したいこともあります。 This can be achieved using ice-modifiers.
上の行には ice 修飾子が含まれており、下の行でプラグインを読み込んでいます。
- ターミナルで以下のコマンドを実行しま す。
zi ice id-as'zsh/plugin' atinit'print "Hello World!"'
zi load z-shell/0
This registered the plugin under the plugin ID zsh/plugin
instead of z-shell/0
. This will work as expected e.g. zi update zsh/plugin
, zi remove zsh/plugin
, etc. プラグインをロードする前に「Hello World!」が表示されます 。
Ice-modifierを増やしてもう一度インストールしましょう。
- ターミナルで以下のコマンドを実行します。
zi ice id-as'final/countdown' \
atinit'+zi-message "{bapo}Cloned!"' \
atclone'+zi-message "{quos}Boom!"' \
atload'+zi-message "{apo}Loaded!"' countdown
zi load z-shell/0
Order of execution
関連する ice-modifier の実行順序は次のとおりです。
atinit'' →
atpull'!' →
make'!!' →
mv'' →
cp'' →
make'!' →
atclone'' / atpull'' →
make'!' →
[ plugin script loading ] →
src'' →
multisrc'' →
atload''
A few remarks
- The syntax automatically detects if the object is a snippet or a plugin, by checking if the object is an URL, i.e.: if it starts with
http*://
orOMZ::
, etc. - To load a local-file snippet (which will be treated as a local-directory plugin by default) use the
is-snippet
ice, - To load a plugin in
light
mode use thelight-mode
ice. - If the plugin name collides with an ice name, precede the plugin name with
@
, e.g.:@sharkdp/fd
(collides with thesh
ice, ZI will take the plugin name assh"arkdp/fd"
), see the next section for an example.
Syntax alternatives
Zi supports alternatives such as the equal (=
) syntax:
zi ice id-as=equal atload="print Hello World"
zi load z-shell/0
The colon (:
) syntax:
zi ice id-as:colon atload:"print Hello World"
zi load z-shell/0
そして、上記の全てと併せて、GNU 構文も同様に利用できます。
zi ice id-as=GNU --atload="print Hello World"
zi load z-shell/0
代わりの構文はVimのようなエディタのハイライトを利用し、文字列とiceの表現を別の色にすることができます。 However, with zi-vim-syntax the syntax definition can be superseded with the highlighting specifically for Zi. syntax definition can be superseded with the highlighting specifically for Zi.
Utilizing "make"
Vim repository on GitHub – a typical source code that needs compilation, Zi can manage the run of ./configure
and other make
stuff. Ice-modifier pick
adds the binary program to $PATH
. You could also install the package under the path $ZPFX.
zi ice as"program" atclone"rm -f src/auto/config.cache; ./configure" \
atpull"%atclone" make pick"src/vim"
zi light vim/vim
The make'…'
ice could also be: make"install PREFIX=$ZPFX"
, if "install" wouldn't be the only, default target.
$ZPFX is provided by Zi, it is set to ~/.zi/polaris
by default. However, it can be changed by specifying the $ZPFX=
target.
zi ice as"program" pick"$ZPFX/bin/git-*" make"PREFIX=$ZPFX"
zi light tj/git-extras
The Makefile
of the project above has only 2 tasks:
- Install the target.
- Build scripts that are required for installation.
The Makefile
with 2 tasks, can use:
make"all install PREFIX=…"
,pick'…'
willchmod +x
all matching files and add$ZPFX/bin/
to$PATH
.
Compiling programs
zi ice as"program" atclone"rm -f src/auto/config.cache; ./configure" \
atpull"%atclone" make pick"src/vim"
zi light vim/vim
構文 | 説明 |
---|---|
as'program' | Add file selected by pick'…' to $PATH , and do not source it. |
atclone'…' | ダウンロード後、任意のコマンドを実行する。 |
atpull'%atclone' | Execute the same code atclone'…' is given, but after successful update. |
make | Run make after atclone'…' and atpull'…' (note: make'!' will execute before them). |
pick'src/vim' | Set the executable flag on src/vim , hint that src/ should be added to $PATH . |
The same but with installation (make install
) under $ZPFX by default:
zi ice as'program' atclone'rm -f src/auto/config.cache; \
./configure --prefix=$ZPFX' atpull'%atclone' make'all install' pick'$ZPFX/bin/vim'
zi light vim/vim
構文 | 説明 |
---|---|
as'program' | As above. |
atclone'…' | As above plus pass --prefix=$ZPFX to ./configure , to set the installation directory. |
atpull'%atclone' | As above. |
make | As above, but also run the install target. |
pick'src/vim' | as above, but for a different path $ZPFX/bin/vim . |
LS_COLORS
A repository trapd00r/LS_COLORS provides a file with color definitions for GNU ls
command, and also for ogham/exa. Typically one does eval $( dircolors -b $HOME/LS_COLORS)
to process this file and set the environment for ls
. This means dircolors
is run by every shell startup. It costs much time to create a fork and program, i.e., the dircolors
binary needs to be loaded to obtain and process the color definitions. The following invocation solves this problem:
zi ice atclone'dircolors -b LS_COLORS > clrs.zsh' \
atpull'%atclone' pick"clrs.zsh" nocompile'!' \
atload'zstyle ":completion:*" list-colors ${(s.:.)LS_COLORS}'
zi light trapd00r/LS_COLORS
構文 | 説明 |
---|---|
atclone'…' | Generate shell script, passing it to eval . More: 1 |
atpull'%atclone' | Do the same at any update of the plugin. More: 2 |
pick"clrs.zsh" | Source the previously generated file clrs.zsh . |
nocompile'!' | Invokes compilation after the atclone'…' and the exclamation mark causes this. |
atload'…' | Additionally sets up the Zsh completion to use the colors provided by the trapd00r package. |
This way, except for the plugin installation and update, dircolors
isn't run, just normal sourcing is done. The everyday sourced file, i.e. clrs.zsh
, is being compiled to speed up the loading.
Direnv
The project direnv/direnv registers itself in the Z shell to modify the environment on directory change. This registration is most often done by eval "$(direnv hook zsh)"
added to .zshrc
.
zi ice as"program" make'!' atclone'./direnv hook zsh > zhook.zsh' \
atpull'%atclone' src"zhook.zsh"
zi light direnv/direnv
make'!'
– executemake
beforeatclone'…'
and beforeatpull'…'
(seemake
above),src'zhook.zsh'
– source filezhook.zsh
.
In general, direnv
works by hooking up to Zsh. The code that does this is provided by the program direnv
(built by make'…'
).
Above atclone'…'
puts this code into file zhook.zsh
, src''
sources it. This way direnv hook zsh
is executed only on clone and update, and Zsh starts faster.
Glance at the 'for' syntax
The drawback of this standard procedure is that the direnv
binary is run on every shell startup and significantly slows it down. Zi allows to solve this in the following way:
zi as"program" make'!' atclone'./direnv hook zsh > zhook.zsh' \
atpull'%atclone' pick"direnv" src"zhook.zsh" for \
direnv/direnv
構文 | 説明 |
---|---|
make'!' | Compile direnv , the exclamation mark means: run the make first, before atclone'…' and atpull'…' hooks. |
atclone'…' | As soon as the plugin is installed generate the registration code and save it to zhook.zsh , instead of passing it to eval . |
atpull'%atclone' | The atclone'…' runs on installation while atpull'…' runs on update of the plugin. |
src'zhook.zsh' | Load generated registration code |
pick'direnv' | Ensure +x permission on the binary |
as'program' | The plugin is a program, there's no main file to the source. |
In this method, the registered code is generated once on every installation or update, then sourced without running direnv
itself. The project is also available as a binary GitHub releases. This distribution can be installed by:
zi from"gh-r" as"program" mv"direnv* -> direnv" \
atclone'./direnv hook zsh > zhook.zsh' atpull'%atclone' \
pick"direnv" src="zhook.zsh" for \
direnv/direnv