-
-
Save bf4648/592cd16a61287bd848c42ddb0f0829a9 to your computer and use it in GitHub Desktop.
Automatically activate/deactivate virtualenv in fish shell
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
# Based on https://gist.github.com/bastibe/c0950e463ffdfdfada7adf149ae77c6f | |
# Changes: | |
# * Instead of overriding cd, we detect directory change. This allows the script to work | |
# for other means of cd, such as z. | |
# * Update syntax to work with new versions of fish. | |
function __auto_source_venv --on-variable PWD --description "Activate/Deactivate virtualenv on directory change" | |
status --is-command-substitution; and return | |
# Check if we are inside a git directory | |
if git rev-parse --show-toplevel &>/dev/null | |
set gitdir (realpath (git rev-parse --show-toplevel)) | |
else | |
set gitdir "" | |
end | |
# If venv is not activated or a different venv is activated and venv exist. | |
if test "$VIRTUAL_ENV" != "$gitdir/.venv" -a -e "$gitdir/.venv/bin/activate.fish" | |
source $gitdir/.venv/bin/activate.fish | |
# If venv activated but the current (git) dir has no venv. | |
else if not test -z "$VIRTUAL_ENV" -o -e "$gitdir/.venv" | |
deactivate | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment