Last active
September 21, 2019 09:08
-
-
Save haf/08ffdeecab4bc1bf6fbed9b3198142ae to your computer and use it in GitHub Desktop.
Dirprofile ZSH plugin — loads a different script depending on active directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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