Skip to content

Instantly share code, notes, and snippets.

@codemonkey2841
Last active August 29, 2015 14:01
Show Gist options
  • Save codemonkey2841/dd95cd8f9c87759f276f to your computer and use it in GitHub Desktop.
Save codemonkey2841/dd95cd8f9c87759f276f to your computer and use it in GitHub Desktop.
Auto workon/deactivate a python virtualenv
function has_virtualenv() {
if [[ $PWD == $PROJECT_HOME* ]]; then
PRJCT_NAME=$(echo ${PWD//$PROJECT_HOME/} | cut -f 2 -d /)
if [ -n "$PRJCT_NAME" ]; then
if [ -e $WORKON_HOME/$PRJCT_NAME ]; then
if [ -z "$VIRTUAL_ENV" ]; then
workon $PRJCT_NAME
fi
return
fi
fi
fi
if [ -n "$VIRTUAL_ENV" ]; then
deactivate &> /dev/null
fi
}
function venv_cd() {
cd "$@" && has_virtualenv
}
alias cd="venv_cd"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment