Zsh:関数にバインドされたキーを遅延ロードしますか?

Zsh:関数にバインドされたキーを遅延ロードしますか?

新しいシェルの起動時にロードされるzshスクリプトがあり、デフォルトはzshです。

func1() {
  <..check if in a git repo, if not return..>
  <..code here..>
}
zle -N func1
bindkey '\eo' func1

func1押すと上記の機能が実行されますAlt-o。この関数はgitリポジトリにのみロードされます。

問題は、この関数のロードにかかるコストがかなり高いことです。新しいシェルの起動時に常にロードされると、シェルプロンプトが遅くなります。 「怠惰な」方法は何ですか?最初のトリガーからのみロードしますAlt-oか?

ベストアンサー1

autoload関数が使用されるまで関数のロードを遅らせるメカニズムです$fpathtestfunc

% < testfunc
testfunc() {
    sleep 3
    print this is a test function
}

次にautoload(必要な場合zle ...):

% grep testfunc ~/.zshrc
autoload -U testfunc

使用するまでメモリに入れないでください。

% exec zsh -l
% print $functions[testfunc]
builtin autoload -XU
% testfunc
this is a test function
% print $functions[testfunc]
    sleep 3
    print this is a test function

おすすめ記事