Skip to content

Instantly share code, notes, and snippets.

@jeetsukumaran
Last active October 20, 2021 21:40
Show Gist options
  • Save jeetsukumaran/f4b7c2fa01ed01027ebfeb9f4bb39d99 to your computer and use it in GitHub Desktop.
Save jeetsukumaran/f4b7c2fa01ed01027ebfeb9f4bb39d99 to your computer and use it in GitHub Desktop.
# Bash completion of conda environment names {{{3
#
# Triggered on:
# - ``conda activate <TAB>``
# - ``conda ... --clone <TAB>``
# - ``conda ... --name <TAB>``
#
# (C) 2021 Jeet Sukumaran
# (Free to use under the MIT License terms)
function _conda_env_complete() {
local cur userhost path prev
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
# _expand || return 0
case "$prev" in
activate|--clone|--name)
if [[ $(type -P conda) ]]
then
condaenvs=$(conda env list | grep -v '#' | awk '{print $1}' | awk -v RS= '{$1=$1}1')
COMPREPLY=($( compgen -W "$condaenvs" -- $cur ) )
else
# echo -e "\n'conda' not found on path"
return 0
fi
;;
esac
return 0
}
complete -F _conda_env_complete conda
#}}}3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment