Created
November 21, 2013 15:30
-
-
Save cjerdonek/7583644 to your computer and use it in GitHub Desktop.
A snippet to automatically call virtualenvwrapper's "workon" command when entering a directory in the shell. This can be added to your .profile, .bash_profile, .bashrc, etc.
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
# Call virtualenvwrapper's "workon" if .venv exists. This is modified from-- | |
# http://justinlilly.com/python/virtualenv_wrapper_helper.html | |
# which is linked from-- | |
# http://virtualenvwrapper.readthedocs.org/en/latest/tips.html#automatically-run-workon-when-entering-a-directory | |
check_virtualenv() { | |
if [ -e .venv ]; then | |
env=`cat .venv` | |
echo "Found .venv in directory. Calling: workon ${env}" | |
workon $env | |
fi | |
} | |
venv_cd () { | |
builtin cd "$@" && check_virtualenv | |
} | |
# Call check_virtualenv in case opening directly into a directory (e.g | |
# when opening a new tab in Terminal.app). | |
check_virtualenv | |
# Add the following to ~/.bash_aliases: | |
# alias cd="venv_cd" |
workon should only be called if the venv isn't already initialized
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would be good to update this so that
venv_cd
wrapscd
ifcd
has already been aliased (without falling into the recursion trap pointed out here by @dorkitude).