diff --git a/plugins/conda/README.md b/plugins/conda/README.md index 70530d01e..0cd88ce55 100644 --- a/plugins/conda/README.md +++ b/plugins/conda/README.md @@ -1,6 +1,6 @@ # conda plugin -The conda plugin provides [aliases](#aliases) for `conda`, usually installed via [anaconda](https://www.anaconda.com/) or [miniconda](https://docs.conda.io/en/latest/miniconda.html). +The conda plugin provides [aliases](#aliases) and [completion](#completion) for `conda`, usually installed via [anaconda](https://www.anaconda.com/) or [miniconda](https://docs.conda.io/en/latest/miniconda.html). To use it, add `conda` to the plugins array in your zshrc file: @@ -28,10 +28,18 @@ plugins=(... conda) | `cnle` | `conda list --export` | Export the list of installed packages in the current environment | | `cnles` | `conda list --explicit > spec-file.txt` | Export the list of installed packages in the current environment to a spec file | | `cnr` | `conda remove` | Remove given package | -| `cnrn` | `conda remove -y -all -n` | Remove all packages in the specified environment | -| `cnrp` | `conda remove -y -all -p` | Remove all packages in the specified prefix | +| `cnrn` | `conda remove -y --all -n` | Remove all packages in the specified environment | +| `cnrp` | `conda remove -y --all -p` | Remove all packages in the specified prefix | | `cnry` | `conda remove -y` | Remove given package without confirmation | | `cnsr` | `conda search` | Search conda repositories for package | | `cnu` | `conda update` | Update conda package manager | | `cnua` | `conda update --all` | Update all installed packages | | `cnuc` | `conda update conda` | Update conda package manager | + +## Completion + +Tab completion is available for the following commands, using the environment list conda keeps in +`~/.conda/environments.txt`: + +- `cna`: completes environment names for `conda activate` +- `cnrn`: completes environment names for `conda remove -y --all -n` diff --git a/plugins/conda/conda.plugin.zsh b/plugins/conda/conda.plugin.zsh index 7a130ba7b..e99999ec3 100644 --- a/plugins/conda/conda.plugin.zsh +++ b/plugins/conda/conda.plugin.zsh @@ -1,4 +1,7 @@ -alias cna='conda activate' +unalias cna cnrn 2>/dev/null +cna() { conda activate "$@"; } +cnrn() { conda remove -y --all -n "$@"; } + alias cnab='conda activate base' alias cncf='conda env create -f' alias cncn='conda create -y -n' @@ -14,10 +17,26 @@ alias cnl='conda list' alias cnle='conda list --export' alias cnles='conda list --explicit > spec-file.txt' alias cnr='conda remove' -alias cnrn='conda remove -y --all -n' alias cnrp='conda remove -y --all -p' alias cnry='conda remove -y' alias cnsr='conda search' alias cnu='conda update' alias cnua='conda update --all' alias cnuc='conda update conda' + +_omz_conda_envs() { + emulate -L zsh -o extended_glob + local envs_file="$HOME/.conda/environments.txt" + [[ -r "$envs_file" ]] || return 1 + + # Each line is an env prefix; named envs live under /envs/. + # The root prefix (base) and envs created with -p have no name and are skipped. + local -aU envs + envs=(${(f)"$(<$envs_file)"}) + envs=(${${(M)envs:#*/envs/[^/]##}:t}) + (( ${#envs} )) && compadd -Q -a envs +} + +if (( $+functions[compdef] )); then + compdef _omz_conda_envs cna cnrn +fi