The $fpath variable is an array used by Zsh to locate shared functions. Unlike $PATH (which is a colon-separated string), $fpath uses space-separated array syntax.
Adding directories to fpath
To add a new directory to the search path without overwriting existing entries, use the += operator:
fpath+=('/some/directory')
Or use the array assignment syntax:
export fpath=($ZDOTDIR/functions $fpath)
Creating and using shared functions
- File Naming: The filename must match the function name. For a function named
blah, create a file named blah. - File Content: Shared function files do not need a function definition wrapper. It is recommended to put
emulate -L zsh at the top of the file to ensure consistent behavior. - Loading: Use the
autoload command to load the function into the shell.
Example Workflow
# 1. Add the functions folder to fpath
export fpath=($ZDOTDIR/functions $fpath)
# 2. Create a function file named 'blah'
$ echo 'echo blah blah' > ${fpath[1]}/blah
# 3. Autoload the function
$ autoload -U blah
# 4. Run the function
$ blah
blah blah
# Add our own functions folder to fpath
export fpath=($ZDOTDIR/functions $fpath)
# Create a file called blah in $ZDOTDIR/functions folder
$ echo 'echo blah blah' > ${fpath[1]}/blah
$ autoload -U blah
$ blah
blah blah