5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Zsh — autoload の基本

Last updated at Posted at 2018-08-28

autoload で何が出来る?

特定のディレクトリにファイルを置くだけで、そのファイル名と同じ名前 でコマンドが使えるようになる。
(コマンドの実体はシェルの関数)

autoload 用のディレクトリを作成

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

ディレクトリを $FPATH に追加する

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

autoload したいファイルを作成する

echo "echo hello zsh autoload" > "$HOME/.zsh/autoload/hello-zsh-autoload"
  • ファイル名がコマンド名になる
  • 実行権限は必要ない
  • ファイル内で関数を作る必要はない

コマンドを autoload する

autoload -U hello-zsh-autoload

コマンドが使えるようになる

$ hello-zsh-autoload
hello zsh autoload

コマンドの実体

hello-zsh-autoload という関数が自動作成されているのが分かる

$ which hello-zsh-autoload

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

.zshrc への追加例

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

Versions

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

Ref.

Links

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?