Skip to content

Instantly share code, notes, and snippets.

@sevas
Last active October 27, 2015 11:14
Show Gist options
  • Save sevas/d1e0acb3781c4fe99052 to your computer and use it in GitHub Desktop.
Save sevas/d1e0acb3781c4fe99052 to your computer and use it in GitHub Desktop.
modified conda.fish, based on https://gist.github.com/jiffyclub/9679788
function condalist -d 'List conda environments.'
for dir in (ls $HOME/anaconda/envs)
echo $dir
end
end
function condactivate -d 'Activate a conda environment' -a cenv
if test -z $cenv
echo 'Usage: condactivate <env name>'
return 1
end
# condabin will be the path to the bin directory
# in the specified conda environment
if test -d $HOME/miniconda3
set condabin $HOME/miniconda3/envs/$cenv/bin
else if test -d $HOME/miniconda
set condabin $HOME/miniconda/envs/$cenv/bin
else
set condabin $HOME/anaconda/envs/$cenv/bin
end
# check whether the condabin directory actually exists and
# exit the function with an error status if it does not
if not test -d $condabin
echo 'Environment not found.'
return 1
end
# deactivate an existing conda environment if there is one
if set -q __CONDA_ENV_ACTIVE
deactivate
end
# save the current path
set -xg DEFAULT_PATH $PATH
# put the condabin directory at the front of the PATH
set -xg PATH $condabin $PATH
set -xg __current_conda_prefix $condabin
# this is an undocumented environmental variable that influences
# how conda behaves when you don't specify an environment for it.
# https://github.com/conda/conda/issues/473
set -xg CONDA_DEFAULT_ENV $cenv
set -xg __original_python_version $PROMPT_PYTHON_VERSION
set -xg PROMPT_PYTHON_VERSION "conda::$cenv"
# flag for whether a conda environment has been set
set -xg __CONDA_ENV_ACTIVE 'true'
return 0
end
function deactivate -d 'Deactivate a conda environment'
if set -q __CONDA_ENV_ACTIVE
# set PATH back to its default before activating the conda env
set -xg PATH $DEFAULT_PATH
set -e DEFAULT_PATH
# unset this so that conda behaves according to its default behavior
set -e CONDA_DEFAULT_ENV
# reset to the original prompt
set -xg PROMPT_PYTHON_VERSION "$__original_python_version"
set -e __original_python_version
set -e __CONDA_ENV_ACTIVE
set -e __current_conda_prefix
end
end
# aliases so condactivate and deactivate can have shorter names
function ca -d 'Activate a conda environment'
condactivate $argv
end
function cda -d 'Deactivate a conda environment'
deactivate $argv
end
function cai -d "Show info on the current environment"
if set -q __CONDA_ENV_ACTIVE
echo $__current_conda_prefix
else
echo "No conda environment activated"
end
end
# complete conda environment names when activating
complete -c condactivate -xA -a "(condalist)"
complete -c ca -xA -a "(condalist)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment