Skip to content

Instantly share code, notes, and snippets.

@dofy
Last active April 6, 2023 08:17
Show Gist options
  • Save dofy/ff397555ae941987a123a81c879b5f8e to your computer and use it in GitHub Desktop.
Save dofy/ff397555ae941987a123a81c879b5f8e to your computer and use it in GitHub Desktop.
Auto run script when you enter any directory.
# Default Command
# Auto run `.auto` script under current work directory
[ -f .auto ] && source .auto
# Other scripts what you want to run
todo

Auto Run Script

A plugin for zsh to Auto Run any script when you enter a directory.

More flexible, more convenient, simpler, and more free.

Usage

  1. open your terminal
  2. create ~/.autorc file
  3. copy .autorc content to your .autorc file and save it
  4. download auto-run.plugin.zsh file to your local folder (maybe ~/.zsh/auto-run.plugin.zsh)
  5. edit your ~/.zshrc file and add following code into it
    [ -f $HOME/.zsh/auto-run.plugin.zsh ] && source $HOME/.zsh/auto-run.plugin.zsh
  6. save ~/.zshrc and exit editor and run source ~/.zshrc
  7. create $PWD/.auto file and write some script then it will be executed when you enter the $PWD folder
# @name: Auto Run Script
# @description: Auto run script when you enter any directory.
# @author: Seven Yu
# @email: dofy.yu@gmail.com
# @version: 2.0.1
# load zsh hook
autoload -U add-zsh-hook
AUTORC=$HOME/.autorc
# auto-run script
auto-run() {
[ -f $AUTORC ] && source $AUTORC 2> /dev/null
}
# add auto-run to chpwd zsh-hook
add-zsh-hook chpwd auto-run
# run auto-run once when zsh start
auto-run
@dofy
Copy link
Author

dofy commented Apr 6, 2023

~/.autorc 是进入任何文件夹都会默认首先执行的脚本

也可以创建 $PWD/.auto 文件,当进入 $PWD 时,若存在 .auto 文件,则会执行的对应的脚本

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment