Skip to content

Instantly share code, notes, and snippets.

@qoda
Last active September 19, 2016 20:03
Show Gist options
  • Save qoda/3236821 to your computer and use it in GitHub Desktop.
Save qoda/3236821 to your computer and use it in GitHub Desktop.
Virtualenv Auto-activate
cd ()
{
builtin cd "$@"
RETVAL=$?
# Test for successful real cd:
if [ 0 -ne $RETVAL ]; then
return $RETVAL
fi
# Deactivate once I move outside the virtualenv directory or
# do nothing when I'm still in this same virtualenv directory
# (Assuming no one nests virtualenvs....)
if [ ! -z "$VIRTUAL_ENV" ]; then
if [ "$PWD" = "${PWD#$VIRTUAL_ENV}" ]; then
deactivate
echo
echo "Deactivating virtualenv..."
else
return 0
fi
fi
# "Detect" virtualenv files:
if [ -r "ve/bin/activate" ]; then
if head -n 1 "ve/bin/activate" | grep -q 'source bin/activate' > /dev/null; then
source "ve/bin/activate"
echo
echo "Activating virtualenv..."
fi
fi
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment