Skip to content

Instantly share code, notes, and snippets.

@tjumyk
Last active December 27, 2023 01:38
Show Gist options
  • Save tjumyk/dd9c20cb29836b712d593a1438151545 to your computer and use it in GitHub Desktop.
Save tjumyk/dd9c20cb29836b712d593a1438151545 to your computer and use it in GitHub Desktop.
bash auto completion for conda
# Shell auto-completion for conda
# For Ubuntu, you may put this file in /etc/bash_completion.d/ or append the contents to ~/.bashrc
_conda_auto_comp()
{
local cur opts
COMPREPLY=()
if [[ ${COMP_CWORD} == 1 ]]; then # list conda commands
cur="${COMP_WORDS[COMP_CWORD]}"
opts="clean config create help info init install list package remove uninstall run search update upgrade activate deactivate"
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
elif [[ ${COMP_CWORD} == 2 ]]; then
if [ ${COMP_WORDS[1]} == 'activate' ]; then # list envs
cur="${COMP_WORDS[COMP_CWORD]}"
opts=$(conda info --envs | grep -v '^$' | grep -v '^#' | cut -d ' ' -f 1) # remove empty lines and comment lines, extract env names
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
fi
fi
}
complete -F _conda_auto_comp conda
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment