Skip to content

Instantly share code, notes, and snippets.

@haf
Last active September 21, 2019 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haf/08ffdeecab4bc1bf6fbed9b3198142ae to your computer and use it in GitHub Desktop.
Save haf/08ffdeecab4bc1bf6fbed9b3198142ae to your computer and use it in GitHub Desktop.
Dirprofile ZSH plugin — loads a different script depending on active directory
# Similar to https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/profiles/profiles.plugin.zsh
# but adapted to suit my needs; basically, source the profile or dotfile if it exists:
# cd ~/dev/qv2
# => /Users/h/.oh-my-zsh/custom/profiles/qv2
# => /Users/h/.oh-my-zsh/custom/profiles/dev.qv2
# => /Users/h/.oh-my-zsh/custom/profiles/h.dev.qv2
# => /Users/h/.oh-my-zsh/custom/profiles/Users.h.dev.qv2
# cd ~/dev/qv2
# => ~/dev/qv2/.zsh_dirprofile
#
# Flag indicating if we've previously run this plugin
typeset -g ZSH_DIRPROFILE
chpwd_functions+=(chpwd_dirprofile)
chpwd_dirprofile() {
[[ "$ZSH_SUBSHELL" != "0" ]] && return
dirprofile_check_profile
dirprofile_check_dotfile
}
dirprofile_check_profile() {
parts=(${(s:/:)PWD})
for i in {${#parts}..1}; do
local profile=${(j:.:)${parts[$i,${#parts}]}}
local file=$ZSH_CUSTOM/profiles/$profile
if [ -f $file ]; then
echo "Sourcing '$file' [profile] (via ~/.oh-my-zsh/custom/plugins/dirprofile/dirprofile.plugin.zsh)"
source $file
fi
done
}
dirprofile_check_dotfile() {
local dotfile="${PWD}/.zsh_dirprofile"
if [[ -f $dotfile ]]; then
echo "Sourcing '$dotfile' [dotfile] (via ~/.oh-my-zsh/custom/plugins/dirprofile/dirprofile.plugin.zsh)"
source $dotfile
fi
}
# Run once on start
[[ -n "$ZSH_DIRPROFILE" ]] && return
chpwd_dirprofile && ZSH_DIRPROFILE=1 || true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment