Skip to content

Instantly share code, notes, and snippets.

@Bouni
Created November 15, 2019 07:16
Show Gist options
  • Save Bouni/2c783e5dfa48860d09896864d1ca00f8 to your computer and use it in GitHub Desktop.
Save Bouni/2c783e5dfa48860d09896864d1ca00f8 to your computer and use it in GitHub Desktop.
Auto activate virtualenv when cd'ing into the parent folder
# https://stackoverflow.com/questions/45216663/how-to-automatically-activate-virtualenvs-when-cding-into-a-directory
function cd() {
builtin cd "$@"
if [[ -z "$VIRTUAL_ENV" ]] ; then
## If env folder is found then activate the vitualenv
if [[ -d ./venv ]] ; then
source ./venv/bin/activate
fi
else
## check the current folder belong to earlier VIRTUAL_ENV folder
# if yes then do nothing
# else deactivate
parentdir="$(dirname "$VIRTUAL_ENV")"
if [[ "$PWD"/ != "$parentdir"/* ]] ; then
deactivate
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment