Skip to content

Instantly share code, notes, and snippets.

@andyhd
Created February 28, 2023 14:02
Show Gist options
  • Save andyhd/25e7df0ac3f5713488c9327c4f730435 to your computer and use it in GitHub Desktop.
Save andyhd/25e7df0ac3f5713488c9327c4f730435 to your computer and use it in GitHub Desktop.
Autoswitch virtualenv on change directory
# autoswitch virtualenv
function cd() {
venv="venv"
if [[ -f .venv ]]; then
if [[ -d "$HOME/.virtualenvs/$(cat .venv)" ]]; then
venv="$HOME/.virtualenvs/$(cat .venv)"
fi
fi
if [[ -d "${venv}" ]] ; then
if type 'deactivate' 2>/dev/null | grep -q 'function'; then
deactivate
fi
fi
builtin cd $@
if [[ -d ${venv} ]] ; then
. "${venv}/bin/activate"
fi
}
cd .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment