Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add conda environment info to terminal prompt

(I'm using anaconda on a MacBook)
By default conda adds the environment info to the comand prompt as follows:

$ source activate my_env

(my_env) $ source deactivate

$

This can be switched off and on using

conda config --set changeps1 (true|false)

Since my terminal prompt is already customised I'd like to add the env info in a different way, but don't know how to exactly.

Right now I'm using the two commands sacand dac in my .bash_profile file to activate and deactivate envs and therefore did this amateurish attempt adding env_var:

env_var=""

#activate env (default env = my_env)
sac() {

    if [ -z $1 ];
    then
        ENV="my_env"
    else
        ENV="${1}"
    fi

    source activate ${ENV}

    env_var="${ENV}"
}

#deactivate env
dac() {
    source deactivate
    env_var=""
}

env_info() {
    if [[ ${env_var} == "" ]]
    then
        echo ""
    else
        echo "in ${env_var}"
    fi
}

PS1="\u "
PS1+="$(env_info) \$";

Which is not working (my bash knowledge is only rudimentary sorry...). env_info always stays "" no matter wether I call sacor dacin the terminal or not.

Question1: Why is the code not working?

Question2: Or is there maybe another way to get the current env-info in a - for this purpose - useful format?
conda info --envs returns to much info...

like image 234
NewNewton Avatar asked Mar 13 '18 17:03

NewNewton


People also ask

How do I change the base environment in Anaconda prompt?

Go to the start menu, right-click 'Anaconda Prompt' and go to file location. Open its properties & change the target to the location of your preferred environment.


2 Answers

The method suggested in the comment from darthbith works very well. The variable $CONDA_DEFAULT_ENV is exactly what I was looking for:

>>> source activate myEnv

>>> echo $CONDA_DEFAULT_ENV
myEnv
like image 107
NewNewton Avatar answered Sep 27 '22 18:09

NewNewton


To add to the answer by A.Wenn, this is what I added to my custom prompt

PS1=""

# Add conda environment to prompt
if [ ! -z "$CONDA_DEFAULT_ENV" ]
then
    PS1+="($CONDA_DEFAULT_ENV) "
fi
like image 29
Aaron Swan Avatar answered Sep 27 '22 17:09

Aaron Swan