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

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

 

posted @   Zhentiw  阅读(185)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-06-18 [Javascript] Correctly Type-Checking Numbers
2019-06-18 [ES2015] Number.isNaN instead of isNaN
2019-06-18 [Javascript] How to deal with floating number
2019-06-18 [Nx] Note for learning Nx
2019-06-18 [Cypress] Wrap External Libraries with Cypress
2017-06-18 [PReact] Handle Simple Routing with preact-router
2017-06-18 [PReact] Reduce the Size of a React App in Two Lines with preact-compat
点击右上角即可分享
微信分享提示