-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconda.completion.bash
More file actions
executable file
·38 lines (35 loc) · 1.81 KB
/
conda.completion.bash
File metadata and controls
executable file
·38 lines (35 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/bin/bash
# Simple conda completion script for bash
_conda_env_completion() {
local cur=${COMP_WORDS[COMP_CWORD]}
local cmd=${COMP_WORDS[1]}
if [[ $cmd == "" ]]; then
local opts=$(conda --help | grep " COMMAND" -A100 | tail -n+2 | cut -d ' ' -f5 | grep -v '^$')
COMPREPLY=($(compgen -W "$opts" -- "$cur"))
elif [[ $cmd == "activate" ]]; then
# Get list of environments, excluding base environment
local envs=$(conda env list --json 2> /dev/null | jq .envs[] -r | tail -n+2 | xargs -I{} basename {})
COMPREPLY=($(compgen -W "$envs" -- "$cur"))
elif [[ $cmd == "deactivate" ]]; then
# Get list of environments, excluding base environment
local envs=$(conda env list --json 2> /dev/null | jq .envs[] -r | tail -n+2 | xargs -I{} basename {})
COMPREPLY=($(compgen -W "$envs" -- "$cur"))
elif [[ $cmd == "remove" ]]; then
# Get list of environments, excluding base environment
local envs=$(conda env list --json 2> /dev/null | jq .envs[] -r | tail -n+2 | xargs -I{} basename {})
COMPREPLY=($(compgen -W "$envs" -- "$cur"))
elif [[ $cmd == "create" ]]; then
# Get create options
local opts=$(conda create --help | grep usage | sed -r 's/usage: conda create |[A-Z|]+//g' | tr -d [ | tr -d ])
COMPREPLY=($(compgen -W "$opts" -- "$cur"))
elif [[ $cmd != "" ]]; then
while IFS= read -r line; do
if grep -Pqv "^ *$line" <<< "$cmd"; then
opts="$opts $line"
fi
done< <(conda --help | grep " COMMAND" -A1000 | tail -n+2 | cut -d ' ' -f5 | grep -v '^$')
COMPREPLY=($(compgen -W "$opts" -- "$cur"))
fi
}
# Register the completion function on the criterion of the conda command being present
which conda >/dev/null && complete -F _conda_env_completion conda