Is it possible to get zsh to autocomplete anaconda environments that show up under source activate
? It is annoying to always have to run conda info -e
to figure out what each environment's name is.
As indicated by asmeurer there is conda-zsh-completion.
Install it by cloning the repository to your machine
git clone https://github.com/esc/conda-zsh-completion
and add the following to your .zshrc
fpath+=/path/to/where/you/installed/conda-zsh-completion
compinit conda
Note that if you're using oh-my-zsh
the first line has to go before the line where you initiate oh-my-zsh
and the second one after the initialisation.
Now you're able to autocomplete conda like
conda a<TAB> env-name-parti<TAB>
For zsh, cited from conda doc, it recommends conda-zsh-completion. But the document from that plugin is not that helpful. I recommend you use antigen, which is a plugin manager for oh-my-zsh. And add this one line in .zshrc
is good to go,
antigen bundle esc/conda-zsh-completion
Assuming you have a standard Oh-My-Zsh installation.
Clone the conda-zsh-completions
repo into ~/.oh-my-zsh/plugins
cd ~/.oh-my-zsh/plugins
git clone https://github.com/esc/conda-zsh-completion
Add the the conda-zsh-completions
plugin to your .zshrc
plugins, e.g.
# Typical .zshrc Oh-My-Zsh content...
plugins = (
# Other plugins
conda-zsh-completion #not conda-zsh-completions
)
Test by typing conda activate
followed by Tab
. You should see a list of your environments.
Following the accepted answer from @asmeurer I found that the code did not work. but if you add a decision. It works.
Here you can copy paste this to add the code to your machine
git clone https://github.com/esc/conda-zsh-completion "${HOME}/conda-zsh-completion"
wait
echo "
# make sure conda autocomplete is after this block
# >>> conda initialize >>>
# <<< conda initialize <<<
# then add here conda autocomplete
# git clone https://github.com/esc/conda-zsh-completion \"${HOME}/conda-zsh-completion\"
fpath+=\"${HOME}/conda-zsh-completion\"
if command -v complete >/dev/null; then # zsh or bash
compinit conda
if command -v compinit >/dev/null; then # bash
complete -F conda
elif command -v compdef >/dev/null; then # zsh
compdef conda
fi
" >> "${HOME}/.zshrc" >> "${HOME}/.bashrc"
EDIT: Though my fix is correct, I discovered a hiccup with conda-zsh-completion where it add a \n error looks something like this
$HOME/.zshrc:###: parse error near `\n'
where ### is the last number of the file, which is impossible. trying to figure out now. I had to disable it in the mean time.
You can make an alias in your ~/.zshrc
file to do this command.
open your ~/.zshrc file in a text editor
add the following line: alias NAME='conda info -e'
you can set whatever name for the alias you want
then save the file and restart your terminal.
You should be able to run NAME
to list the environment name
conda info -e
with fewer keystrokes. –
Hydrosphere © 2022 - 2024 — McMap. All rights reserved.
activate
. – Wingate