Skip to content

Instantly share code, notes, and snippets.

@YumaInaura
Last active November 16, 2023 00:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YumaInaura/20ef6619ed66fa76f7649166bcf3d16e to your computer and use it in GitHub Desktop.
Save YumaInaura/20ef6619ed66fa76f7649166bcf3d16e to your computer and use it in GitHub Desktop.
Zsh — autoload First Steps

Zsh — autoload First Steps

What is autoload?

  • Add file in specified directory and enable to use command.
    • Shell function behaves like command.
  • Command name and file name is pair.

Make autoload directory

mkdir -p "$HOME/.zsh/autoload"

Any path you like.

Add directory to $FPATH

export FPATH="$HOME/.zsh/autoload/:$FPATH"

Add file in autoload directory

echo "echo hello zsh autoload" > "$HOME/.zsh/autoload/hello-zsh-autoload"
  • File name will be command name.
  • No need excutable permission.
  • No need define function in file.

Add new command by autoload

autoload -U hello-zsh-autoload

Now you can hit new command

$ hello-zsh-autoload
hello zsh autoload

Confirm

You can see a new command hello-zsh-autoload was created as function by automatic.

$ which hello-zsh-autoload

hello-zsh-autoload () {
	echo hello zsh autoload
}

Examole. Add to .zshrc

export FPATH="$HOME/.zsh/autoload/:$FPATH"
autoload -U hello-zsh-autoload

Anytime you can use autoloaded command.

Versions

  • zsh 5.5.1 (x86_64-apple-darwin17.5.0)

Links

#!/usr/bin/env zsh -eu
# Usage
# $ zsh autoload.zsh
# or
# $ source autoload.zsh
# $ hello-zsh-autoload
tmp_dir="$HOME/tmp/autoload"
mkdir -p "$tmp_dir"
export FPATH="$tmp_dir:$FPATH"
echo "echo hello zsh autoload" > "$tmp_dir/hello-zsh-autoload"
autoload -U hello-zsh-autoload
hello-zsh-autoload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment