Last Updated: February 25, 2016
· robjens

ZSH autoloading functions I

Z-shells auto-loadable functions are arguably one of the flagships of the shell: using this mechanism will greatly improve your resource management, making it more efficient by only loading functions when they are called. Before this even can take place, zsh will need to have a few things in place. For one, it needs to know the locations where to find these function definitions and some kind of convention for allowing the mechanism which perhaps is best described as a (lazily) loading of function headers (name declarations) and empty bodies.

Let's say you want to create your own zsh framework a la oh-my-zsh and as such start from scratch. We'll call the framework zomg after the famous internet typo abbreviation meme and at the same time honoring the long held zsh tradition of prefixing almost every component with a z.

We'll need a directory to keep our functions:

mkdir -p ~/.zomg/fns

Next we'll need to add this directory to the zsh function path:

echo 'fpath=(~/.zomg/fns $fpath)' >> ~/.zshrc

The technique used here is sometimes called 'rehashing' and is commonly found in *nix environments where a 'shim' or replacement functionality is enforced by overriding the name on the $PATH due to it being in order of precedence from right to left side. Hence our custom ~/.zomg/fns path will override any similar named functions from the core if we want or need to. Having added our $fpath, any functions placed inside (1 per file) is usable as autoload function.

Next fire up your favorite text editor and create a new function file. The convention is such that the name of the file will become the name of the function.

${EDITOR} ~/.zomg/fns/acme

Will create the autoload function acme (hence we do not postfix the name with .zsh since function names don't contain extension and conversely our file doesn't either. Placing a shebang is required nor common either.

For now, our function will be very basic but it will - to illustrate - include the actual application of a zsh native function as autoloaded from the $fpath which got setup when you installed zsh. Populate the file with some content as:

# ~/.zomg/fns/acme

autoload colors; colors
rc=$reset_color # because I'm laaaaaazy

print -- $fg[blue] \⚛ $rc "$@"

Let's break that apart for a sec shall we?

  1. The autoload colors will autoload the function (header only with empty body).
  2. The ; means a new expression like always, while colors actually is the function call.
  3. Since all colors does is expose color variables to us, we can use them and assign a shortcut $rc for $reset_color exposed variable.
  4. Other variables are also available, one example is $fg[color] for the foreground color

Exit the file and now reload your shell. Then try out the function:

source ~/.zshrc
autoload acme
acme "KABOOM! ... Nuclear Coyote"

You should see a colored unicode atomic sign and the message printed to the console.

Next pro-tip I'll share a bit more on how to used nested functions in these files to better organize humongous functions.

1 Response
Add your response

" Placing a shebang is required nor common either."
Maybe 'NOT required' ??

over 1 year ago ·