|
| 1 | +# TAGLINE |
| 2 | + |
| 3 | +Autoload shell functions for lazy loading |
| 4 | + |
| 5 | +# TLDR |
| 6 | + |
| 7 | +**Autoload a function** by name |
| 8 | + |
| 9 | +```autoload [function_name]``` |
| 10 | + |
| 11 | +**Autoload with immediate undefined-function handling** (zsh) |
| 12 | + |
| 13 | +```autoload -U [function_name]``` |
| 14 | + |
| 15 | +**Autoload with zsh-style function initialization** (suppresses alias expansion) |
| 16 | + |
| 17 | +```autoload -Uz [function_name]``` |
| 18 | + |
| 19 | +**Autoload all functions** in a directory added to fpath |
| 20 | + |
| 21 | +```autoload -Uz $fpath[1]/*(.:t)``` |
| 22 | + |
| 23 | +**Autoload the completion system** |
| 24 | + |
| 25 | +```autoload -Uz compinit && compinit``` |
| 26 | + |
| 27 | +**Autoload the prompt system** |
| 28 | + |
| 29 | +```autoload -Uz promptinit && promptinit``` |
| 30 | + |
| 31 | +# SYNOPSIS |
| 32 | + |
| 33 | +**autoload** [_-UXmtz_] [_name ..._] |
| 34 | + |
| 35 | +# PARAMETERS |
| 36 | + |
| 37 | +**-U** |
| 38 | +> Suppress alias expansion when the function is loaded |
| 39 | +
|
| 40 | +**-z** |
| 41 | +> Use zsh-style function definitions (default in zsh) |
| 42 | +
|
| 43 | +**-k** |
| 44 | +> Use ksh-style function definitions |
| 45 | +
|
| 46 | +**-X** |
| 47 | +> Immediately load and execute the function (used inside the function itself) |
| 48 | +
|
| 49 | +**-t** |
| 50 | +> Enable execution tracing for the autoloaded function |
| 51 | +
|
| 52 | +**-m** |
| 53 | +> Treat arguments as patterns for matching function names |
| 54 | +
|
| 55 | +**+X** |
| 56 | +> Force the function to be loaded immediately without executing it |
| 57 | +
|
| 58 | +# DESCRIPTION |
| 59 | + |
| 60 | +**autoload** marks shell function names for deferred loading. Instead of reading a function definition into memory at startup, the shell records only the function name. When the function is first called, the shell searches the directories listed in **$fpath** for a file matching the function name, reads its definition, and executes it. |
| 61 | + |
| 62 | +This mechanism significantly reduces shell startup time when many functions are available but rarely used. The function file should contain the function body directly (not wrapped in a `function name { }` block for zsh-style autoloading). |
| 63 | + |
| 64 | +**autoload** is essential for zsh's completion system (**compinit**), prompt themes (**promptinit**), and many other standard functions distributed with zsh. |
| 65 | + |
| 66 | +# CAVEATS |
| 67 | + |
| 68 | +The function file must be in a directory listed in **$fpath**. The file name must exactly match the function name. Functions autoloaded with **-U** will not have aliases expanded in their definitions, which is strongly recommended to avoid unexpected behavior. Only available as a shell builtin in zsh and ksh. |
| 69 | + |
| 70 | +# HISTORY |
| 71 | + |
| 72 | +**autoload** originates from **ksh** (Korn Shell), designed by **David Korn** at Bell Labs in the **1980s**. Zsh adopted and extended the concept, making it central to its modular function system. The **-U** and **-z** flags are zsh-specific additions. |
| 73 | + |
| 74 | +# SEE ALSO |
| 75 | + |
| 76 | +[zsh](/man/zsh)(1), [source](/man/source)(1), [compinit](/man/compinit)(1) |
0 commit comments