Created
August 15, 2011 17:39
-
-
Save leafstorm/1147279 to your computer and use it in GitHub Desktop.
Better activate function for fish
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
# drop this in your ~/.config/fish/functions | |
function activate --description "Activate a virtual environment" | |
# find the target | |
set target $argv[1] | |
if test -z $target | |
set target $PWD | |
end | |
# expand the path in $target | |
set here $PWD | |
cd $target | |
set target $PWD | |
cd $here | |
set -e here | |
# validate the virtualenv | |
if not test -d $target/bin | |
echo "Invalid virtualenv: bin does not exist" | |
return 1 | |
end | |
# create the deactivate function | |
function deactivate --description "Deactivate the virtualenv" | |
if set -q _OLD_VIRTUAL_PATH | |
set -gx PATH $_OLD_VIRTUAL_PATH | |
set -e _OLD_VIRTUAL_PATH | |
end | |
if set -q _OLD_VIRTUAL_FISH_PROMPT_HOSTNAME | |
set -gx __fish_prompt_hostname $_OLD_VIRTUAL_FISH_PROMPT_HOSTNAME | |
set -e _OLD_VIRTUAL_FISH_PROMPT_HOSTNAME | |
end | |
set -e VIRTUAL_ENV | |
if test "$argv[1]" != nondestructive | |
functions -e deactivate | |
end | |
end | |
# clean up any old virtualenvs | |
deactivate nondestructive | |
# set the basic variables | |
set -gx VIRTUAL_ENV $target | |
set -g _OLD_VIRTUAL_PATH $PATH | |
set -gx PATH $VIRTUAL_ENV/bin $PATH | |
set -gx PIP_RESPECT_VIRTUALENV true | |
set -g _OLD_VIRTUAL_FISH_PROMPT_HOSTNAME $__fish_prompt_hostname | |
set -l _ve_basename (basename $VIRTUAL_ENV) | |
if test $_ve_basename = env | |
set -l _ve_basename (basename (dirname $VIRTUAL_ENV)) | |
end | |
set -g __fish_prompt_hostname "$__fish_prompt_hostname [$_ve_basename]" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment