Skip to content

Instantly share code, notes, and snippets.

@xiaohk
Last active April 11, 2017 01:09
Show Gist options
  • Save xiaohk/f1dfbbd156aec873cc315d5a27443503 to your computer and use it in GitHub Desktop.
Save xiaohk/f1dfbbd156aec873cc315d5a27443503 to your computer and use it in GitHub Desktop.
Automatically activate virtualenv when cd into project directory
# Automatically activate virtualenv when using 'cd' (rewrite builtin cd function)
# Assuming current working directory is in a sub-directory of virtualenv directory
function cd {
builtin cd "$@"
# Check upper 4 directories
if [-d "../bin" -a -d "../lib" -a -d "../include" -a -e "../.Python" \
-a -e "../pip-selfcheck.json" ] ; then
source ../bin/activate
elif [ -d "../../bin" -a -d "../../lib" -a -d "../../include" -a -e \
"../../.Python" -a -e "../../pip-selfcheck.json" ] ; then
source ../../bin/activate
elif [ -d "../../../bin" -a -d "../../../lib" -a -d "../../../include" \
-a -e "../../../.Python" -a -e "../../../pip-selfcheck.json" ] ; then
source ../../../bin/activate
elif [ -d "../../../../bin" -a -d "../../../../lib" -a -d \
"../../../../include" -a -e "../../../../.Python" -a -e \
"../../../../pip-selfcheck.json" ] ; then
source ../../../../bin/activate
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment