-
-
Save euphoris/3405460 to your computer and use it in GitHub Desktop.
[virtualenvwrapper] automatic workon hook on cd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export PROJECTS_HOME="$HOME/Projects" | |
function has_virtualenv__() { | |
if [[ ${PWD##$PROJECTS_HOME} != $PWD ]]; then | |
IFS="/" read -ra ADDR <<< "${PWD##$PROJECTS_HOME}" | |
venvname=${ADDR[1]} | |
cur_env=${VIRTUAL_ENV##$WORKON_HOME} | |
if [[ $venvname != "" ]] && [[ -d "$WORKON_HOME/$venvname" ]]; then | |
if [[ ${cur_env:1} != $venvname ]]; then | |
workon "$venvname" | |
fi | |
else | |
if [[ $VIRTUAL_ENV != "" ]]; then | |
deactivate | |
fi | |
fi | |
else | |
if [[ $VIRTUAL_ENV != "" ]]; then | |
deactivate | |
fi | |
fi | |
} | |
function venv_cd() { | |
builtin cd "$@" && has_virtualenv__ | |
} | |
alias cd=venv_cd |
@juanpabloaj that only allows you to cd on workon, but not to workon on cd.
@euphoris with ZSH, you can define a chpwd function that will be called on each working directory change. Also note that read -a
is a GNU extension which does not work out of the box on OS X and BSD. I used echo "${PWD##$PROJECTS_HOME}" | cut -d'/' -f2)
instead.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@euphoris virtualenvwrapper have hooks http://virtualenvwrapper.readthedocs.org/en/latest/scripts.html