Skip to content

Instantly share code, notes, and snippets.

@vsyrovat
Last active November 26, 2020 10:20
Show Gist options
  • Save vsyrovat/684658bacb55ed869265370c2db7ec5a to your computer and use it in GitHub Desktop.
Save vsyrovat/684658bacb55ed869265370c2db7ec5a to your computer and use it in GitHub Desktop.
# Based on https://gist.github.com/laggardkernel/6cb4e1664574212b125fbfd115fe90a4
# create a PROPMT_COMMAND equivalent to store chpwd functions
typeset -g CHPWD_COMMAND=""
_chpwd_hook() {
shopt -s nullglob
local f
# run commands in CHPWD_COMMAND variable on dir change
if [[ "$PREVPWD" != "$PWD" ]]; then
local IFS=$';'
for f in $CHPWD_COMMAND; do
"$f"
done
unset IFS
fi
# refresh last working dir record
export PREVPWD="$PWD"
}
# add `;` after _chpwd_hook if PROMPT_COMMAND is not empty
PROMPT_COMMAND="_chpwd_hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
_phpbrew_use_on_cwd_change() {
[ -e .php-version ] && phpbrew use `cat .php-version`
}
# On directory change
CHPWD_COMMAND="${CHPWD_COMMAND:+$CHPWD_COMMAND;}_phpbrew_use_on_cwd_change"
# On bash start
_phpbrew_use_on_cwd_change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment