Skip to content

Instantly share code, notes, and snippets.

@meise
Last active May 21, 2017 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save meise/5e9119685ce43ea22d4b7a3c93481844 to your computer and use it in GitHub Desktop.
Save meise/5e9119685ce43ea22d4b7a3c93481844 to your computer and use it in GitHub Desktop.
Configure bash variables based on directories
if [[ -e $HOME/.zsh/chpwd_profiles.zsh ]]; then
source $HOME/.zsh/chpwd_profiles.zsh
zstyle ":chpwd:profiles:/home/${USER}/projects(|/|/*)" profile projects
zstyle ":chpwd:profiles:(|/|/*)" profile default
# configuration for profile 'projects':
chpwd_profile_projects() {
[[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
print "chpwd(): Switching to profile: $profile"
export GIT_AUTHOR_EMAIL="foo@bar"
}
# configuration for profile 'default':
chpwd_profile_default() {
[[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
print "chpwd(): Switching to profile: $profile"
export GIT_AUTHOR_EMAIL="default@localhost.local"
}
fi
# http://git.grml.org/?p=grml-etc-core.git;a=blob_plain;f=etc/zsh/zshrc;hb=HEAD
#
# chpwd_profiles(): Directory Profiles, Quickstart:
#
# In .zshrc.local:
#
# zstyle ':chpwd:profiles:/usr/src/grml(|/|/*)' profile grml
# zstyle ':chpwd:profiles:/usr/src/debian(|/|/*)' profile debian
# chpwd_profiles
#
# For details see the `grmlzshrc.5' manual page.
#function chpwd_profiles() {
# local profile context
# local -i reexecute
#
# context=":chpwd:profiles:$PWD"
# zstyle -s "$context" profile profile || profile='default'
# zstyle -T "$context" re-execute && reexecute=1 || reexecute=0
#
# if (( ${+parameters[CHPWD_PROFILE]} == 0 )); then
# typeset -g CHPWD_PROFILE
# local CHPWD_PROFILES_INIT=1
# (( ${+functions[chpwd_profiles_init]} )) && chpwd_profiles_init
# elif [[ $profile != $CHPWD_PROFILE ]]; then
# (( ${+functions[chpwd_leave_profile_$CHPWD_PROFILE]} )) \
# && chpwd_leave_profile_${CHPWD_PROFILE}
# fi
# if (( reexecute )) || [[ $profile != $CHPWD_PROFILE ]]; then
# (( ${+functions[chpwd_profile_$profile]} )) && chpwd_profile_${profile}
# fi
#
# CHPWD_PROFILE="${profile}"
# return 0
# chpwd_functions=( ${chpwd_functions} chpwd_profiles )
#}
if [[ $ZSH_VERSION == 4.3.<3->* || $ZSH_VERSION == 4.<4->* || $ZSH_VERSION == <5->* ]] ; then
CHPWD_PROFILE='default'
function chpwd_profiles()
{
local -x profile
zstyle -s ":chpwd:profiles:${PWD}" profile profile || profile='default'
if (( ${+functions[chpwd_profile_$profile]} )) ; then
chpwd_profile_${profile}
fi
CHPWD_PROFILE="${profile}"
return 0
}
chpwd_functions=( ${chpwd_functions} chpwd_profiles )
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment