Search code examples
zsh

What does autoload do in zsh?


I wasn't able to find a documentation for the widely used autoload command in zsh. Does anybody can explain it in plain English?

A bit more specific: What does autoloading of modules mean, for example in this line:

autoload -Uz vcs_info

What does it do?


I've tried autoload --help, man autoload, googling - no success. Thanks!


Solution

  • The autoload feature is not available in bash, but it is in ksh (korn shell) and zsh. On zsh see man zshbuiltins.

    Functions are called in the same way as any other command. There can be a name conflict between a program and a function. What autoload does is to mark that name as being a function rather than an external program. The function has to be in a file on its own, with the filename the same as the function name.

    autoload -Uz vcs_info
    

    The -U means mark the function vcs_info for autoloading and suppress alias expansion. The -z means use zsh (rather than ksh) style. See also the functions command.

    Edit (from comment, as suggested by @ijoseph):

    So it records the fact that the name is a function and not an external program - it does not call it unless the -X option is used, it just affects the search path when it is called. If the function name does not collide with the name of a program then it is not required. Prefix your functions with something like f_ and you will probably never need it.

    For more detail see http://zsh.sourceforge.net/Doc/Release/Functions.html.