Skip to content

Instantly share code, notes, and snippets.

@bf4648
Forked from tommyip/venv.fish
Created October 10, 2022 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bf4648/592cd16a61287bd848c42ddb0f0829a9 to your computer and use it in GitHub Desktop.
Save bf4648/592cd16a61287bd848c42ddb0f0829a9 to your computer and use it in GitHub Desktop.
Automatically activate/deactivate virtualenv in fish shell
# 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