Skip to content

Instantly share code, notes, and snippets.

@kn
Created September 23, 2013 23:29
Show Gist options
  • Save kn/6678405 to your computer and use it in GitHub Desktop.
Save kn/6678405 to your computer and use it in GitHub Desktop.
###
# Automatically activate virtualenv if appropriate.
# This assumes virtualenvs are either:
# - in $WORKON_HOME folder, in which case it uses workon command to activate the virtualenv.
# - in ./local folder, in which case it uses ./setenv.sh to activate the virtualenv.
# Note: This will have a conflict if other script overrides cd command.
# e.g. rvm overrides cd command for local .rvm file.
###
cd() {
if builtin cd "$@"; then
if [[ -s ./setenv.sh ]]; then
if [[ "$(basename ./${VIRTUAL_ENV})" != "local" ]]; then
if [[ "$@" != "." ]]; then
source ./setenv.sh "";
fi
fi
elif [[ -s ${WORKON_HOME}/$(basename ${PWD}) ]]; then
if [[ "$(basename ./${VIRTUAL_ENV})" != "$(basename ${PWD})" ]]; then
workon $(basename ${PWD});
fi
fi
else
return $?;
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment