Skip to content

Instantly share code, notes, and snippets.

@zachmccormick
Created February 19, 2015 23:51
Show Gist options
  • Select an option

  • Save zachmccormick/c76714bc9259fbf22c38 to your computer and use it in GitHub Desktop.

Select an option

Save zachmccormick/c76714bc9259fbf22c38 to your computer and use it in GitHub Desktop.
Automagically set virtualenv (either folder name or .venv file)
function check_for_virtual_env {
[ -d .git ] || git rev-parse --git-dir &> /dev/null
if [ $? == 0 ]; then
local ENV_NAME=`basename \`pwd\``
if [ -e .venv ]; then
ENV_NAME=`cat .venv`
fi
if [ "${VIRTUAL_ENV##*/}" != $ENV_NAME ] && [ -e $WORKON_HOME/$ENV_NAME/bin/
activate ]; then
workon $ENV_NAME && export CD_VIRTUAL_ENV=$ENV_NAME
fi
elif [ $CD_VIRTUAL_ENV ]; then
deactivate && unset CD_VIRTUAL_ENV
fi
}
function cd {
builtin cd "$@" && check_for_virtual_env
}
check_for_virtual_env
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment