Skip to content

Instantly share code, notes, and snippets.

@gibatronic
Last active June 24, 2019 09:10
Show Gist options
  • Save gibatronic/de66e3841b981798e6c1 to your computer and use it in GitHub Desktop.
Save gibatronic/de66e3841b981798e6c1 to your computer and use it in GitHub Desktop.
Automatically run workon when entering a directory
function check_for_virtual_env {
[ -d .git ] || git rev-parse --git-dir &> /dev/null
if [ $? == 0 ]; then
local ENV_NAME=`basename \`pwd\``
if [ "${VIRTUAL_ENV##*/}" != $ENV_NAME ] && [ -e $WORKON_HOME/$ENV_NAME/bin/activate ]; then
workon $ENV_NAME && export CD_VIRTUAL_ENV=$ENV_NAME
fi
elif [ $CD_VIRTUAL_ENV ]; then
deactivate && unset CD_VIRTUAL_ENV
fi
}
function cd {
builtin cd "$@" && check_for_virtual_env
}
check_for_virtual_env
@BGBRWR
Copy link

BGBRWR commented Nov 1, 2018

I had to replace the == on this line to -eq to make it work on zsh.

Other than that, it's awesome, thanks!

Updated to work on zsh.
https://gist.github.com/BGBRWR/82e66547d7013f3ae687eb792b6b7e20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment