Skip to content

Instantly share code, notes, and snippets.

@kittinan
Created June 17, 2019 02:28
Show Gist options
  • Save kittinan/8617916b36c1d691d027bf2d4011e416 to your computer and use it in GitHub Desktop.
Save kittinan/8617916b36c1d691d027bf2d4011e416 to your computer and use it in GitHub Desktop.
auto activate virtualenv when enter to folder has venv folder
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