zsh autocomplete anaconda environments
Asked Answered
L

5

18

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.

Lunch answered 4/8, 2015 at 20:15 Comment(2)
There is github.com/esc/conda-zsh-completion but I don't know if it supports activate.Wingate
are you using windows or mac?Papuan
C
13

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>
Chism answered 4/9, 2019 at 17:49 Comment(0)
U
5

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
Undertone answered 12/5, 2020 at 17:49 Comment(0)
J
3

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.

Janson answered 19/12, 2023 at 2:9 Comment(0)
P
1

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.

Polyester answered 20/1, 2023 at 12:46 Comment(0)
P
-3

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

Papuan answered 22/8, 2018 at 6:52 Comment(2)
This doesn't answer the question, it just lets you do conda info -e with fewer keystrokes.Hydrosphere
He was getting annoyed because he kept having to run a long command, does an alias solve that problem?Papuan

© 2022 - 2024 — McMap. All rights reserved.