diff --git a/_conda b/_conda index ef49d69..36a7f48 100644 --- a/_conda +++ b/_conda @@ -49,6 +49,21 @@ # # zstyle ":conda_zsh_completion:*" show-unnamed true # +# To display environments autocompletion sorted in creation order add the following +# to your '.zshrc': +# +# zstyle ":conda_zsh_completion:*" sort-envs-by-time true +# +# The completion will display both global environments (envs located in conda_dir/envs and base env) +# and local environments (located in ~/.conda/envs). +# If enables sort-envs-by-time, it will display local environments first. +# To display global environments first, add the following to your '.zshrc': +# +# zstyle ":conda_zsh_completion:*" show-global-envs-first true +# +# If not enables sort-envs-by-time, then all environments will be sorted by alphabetical order, +# and this option is useless. +# # To forcefully clean the completion cache, look in '~/.zcompcache' and remove # file starting with 'conda_'. And/or do 'rm ~/.zcompdump*'. # @@ -138,11 +153,37 @@ local state line context local -A opt_args __conda_envs(){ - local -a envs + local -a envs unnamed sort globalfirst localenvs globalenvs + local -a ls_opts=("-1") + local -a describe_opts # only parse environments.txt (including unnamed envs) if asked by the user - zstyle -s ":conda_zsh_completion:*" show-unnamed tmp - envs=($(echo base && ls -1 ${${CONDA_EXE}%bin/conda}/envs && (test -n "$tmp" && cat ${HOME:?}/.conda/environments.txt) | cut -f1 -d' ' | sed -e "s|^${HOME:?}/.conda/envs/||")) - _describe -t envs 'conda environments' envs + zstyle -s ":conda_zsh_completion:*" show-unnamed unnamed + zstyle -s ":conda_zsh_completion:*" sort-envs-by-time sort + zstyle -s ":conda_zsh_completion:*" show-global-envs-first globalfirst + if test -n "$sort"; then + ls_opts+=("-t") + describe_opts+=("-V") + fi + + # global envs (if exists) and base env. + globalenvs=($([[ -d "${${CONDA_EXE}%bin/conda}/envs" ]] && ls $ls_opts ${${CONDA_EXE}%bin/conda}/envs)) + globalenvs+=("base") + + # local envs (if exists). + localenvs=($([[ -d "${HOME:?}/.conda/envs" ]] && ls $ls_opts ${HOME:?}/.conda/envs)) + + if test -n "$globalfirst"; then + envs=($globalenvs $localenvs) + else + envs=($localenvs $globalenvs) + fi + + # unmaned envs (if show-unammed). + if test -n "$unnamed"; then + envs+=($( (test -n "$unnamed" && cat ${HOME:?}/.conda/environments.txt) | cut -f1 -d' ' | sed -e "s|^${HOME:?}/.conda/envs/||")) + fi + + _describe $describe_opts -t envs 'conda environments' envs } __conda_packages_installed(){