Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Source a file in zsh when entering a directory

Is there a way to source a particular file to set up the environment when entering a particular directory? Sort of like what rvm does, but more general.

like image 642
Daisy Sophia Hollman Avatar asked Jun 11 '13 18:06

Daisy Sophia Hollman


1 Answers

IMHO you should not use an alias for this but add a hook to any directory change:

autoload -U add-zsh-hook
load-local-conf() {
     # check file exists, is regular file and is readable:
     if [[ -f .source_me && -r .source_me ]]; then
       source .source_me
     fi
}
add-zsh-hook chpwd load-local-conf

This hook function will run on any directory change.

FWIW, should you wish to change dirs without trigering the hooks, use cd -q dirname

like image 88
Francisco Avatar answered Oct 17 '22 19:10

Francisco