0

If I load completion functions at shell startup, but I spawn shell like a madman to work on the same folder.

This is the case when I have multiple completion functions that target repository that contain .git folder, if we're in repo then the functions are loaded.

Problem is that with all completion functions start when spawn new shell, it draw shell prompt quite slow.

So how to manually trigger to load/source completion functions? Even I know I already in .git repo, I still want manually load completion function before use it, the cost of slow shell spawn is higher than the completion itself.

2

Instead of loading completion functions at startup, declare them as autoloaded. This way each function will be loaded the first time it is used.

The normal way to do this is to put the code of each function into a file in a directory on $fpath. Call the file with the name you want to give to the function. On the first line of the file, add a #compdef tag.

So if you used to have this in your .zshrc:

autoload -Uz compinit
compinit -u
_foo () {
  code goes here
}
compdef _foo foo

then, instead, only put this in your .zshrc:

fpath=(~/.zsh/functions $fpath)
autoload -Uz compinit
compinit -u

and create ~/.zsh/functions/_foo containing

#compdef foo
code goes here
  • So each time I want to use the _foo function I have to issue this command autoload -Uz _foo? If yes, is there a way to map to a combined key to call it faster? – Tuyen Pham Jan 26 at 4:57
  • @TuyenPham You don't need an explicit autoload statement if the function is in a file in $fpath when you run compinit and the file starts with a #compdef or #autoload line. – Gilles Jan 26 at 14:48
  • In a general file named utils.zsh, can I have multiple #compdef foo and later compdef bar in the same utils.zsh file? I want to have ony one file for multiple small utils. – Tuyen Pham Jan 27 at 6:34
  • @TuyenPham You can have multiple names after #compdef, but that gives you one function that's used to complete multiple commands. You can't put multiple functions in the same file (except auxiliary functions for the main function, but only the main function gets autoloaded). – Gilles Jan 27 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.