首页 > 其他 > 详细

[Zsh] Store Zsh Functions in Individual Files Inside a Directory

时间:2020-06-18 15:03:46      阅读:73      评论:0      收藏:0      [点我收藏+]

Many people just drop all of their Zsh functions inside of their .zshrc and forget about it. But as your collection of functions begins to grow, you start looking for a better way to organize everything. Since Zsh provides autoload for loading in functions dynamically, we can drop all of our functions into individual files, loop through that directory, then autoload each file as a function.

 

Create a folder inside your home directory:

mkdir ~/.zshfunctions

 

Create a hello file inside .zshfunctions:

cd .zshfunctions
vim hello

Just put content:

echo "hello"

 

You can create more files, such as rename-dir:

#path/to/current/dir
# will only return ‘dir‘
currentDir=echo ${PWD##*/}
cd ..
mv $currentDir $1
cd $1

 

Now if you run:

for file in ~/.zshfunctions/*

> echo $file

It will output each file name.

 

If we want to apply rename-dir and hello commands, we just created:

for file in ~/.zshfunctions/*
autoload $file

 


 

If we want to load all the functions when zsh start:

we can modify ~/.zshrc file, add:

for file in ~/.zshfunctions/*;
    do autoload $file
done

 

[Zsh] Store Zsh Functions in Individual Files Inside a Directory

原文:https://www.cnblogs.com/Answer1215/p/13157526.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!