1

To make my command line prompt look prettier on zsh, I added this line to .zshrc:

PROMPT='%F{green}%n%f %B%F{blue}%1~%f%b $ '

However, when I activate an anaconda virtual environment (i.e. conda activate base), I see the name of the virtual environment on the left like this:

(base) myusername ~ $

I wonder how I can modify the virtual environment part of the prompt (say we want to color it to cyan for example)

2
  • hint: oh-my-zsh does all this for you :) Jun 27, 2021 at 17:07
  • @MarcusMüller thank you for the suggestion! I think I am looking for a way to do that without installing oh-my-zsh. Jun 27, 2021 at 18:37

2 Answers 2

2

The first step is to disable the default conda prompt modifier by running conda config --set changeps1 false as they already mentioned.

Next, add the following to your .zshrc:

# Determines prompt modifier if and when a conda environment is active
precmd_conda_info() {
  if [[ -n $CONDA_PREFIX ]]; then
      if [[ $(basename $CONDA_PREFIX) == "miniconda3" ]]; then
        # Without this, it would display conda version
        CONDA_ENV="(base) "
      else
        # For all environments that aren't (base)
        CONDA_ENV="($(basename $CONDA_PREFIX)) "
      fi
  # When no conda environment is active, don't show anything
  else
    CONDA_ENV=""
  fi
}

# Run the previously defined function before each prompt
precmd_functions+=( precmd_conda_info )

# Allow substitutions and expansions in the prompt
setopt prompt_subst

PROMPT='%F{cyan}$CONDA_ENV%f%F{green}%n%f %B%F{blue}%1~%f%b $ '

With this, the conda environment is shown before the rest of the prompt, inside parentheses, and in cyan.

If you want it to appear bold, enclose that portion in %B and %b:

PROMPT='%B%F{cyan}$CONDA_ENV%b%f%F{green}%n%f %B%F{blue}%1~%f%b $ '

If you want to use more colours, check whether your terminal supports it by running echo $TERM. If it returns xterm-256color, you can replace the colours in curly brackets by values from 0 to 255. You can check out the colours here.

PS- Those using anaconda instead of miniconda will need to replace miniconda3 with anaconda3 in the code.

1
  • Thank you, this worked fine.
    – mrVerma
    Dec 23, 2021 at 17:21
0

Run:

conda config --set changeps1 False

or add changeps1: False to your .condarc.

1
  • Thank you for the suggestion, it helps me with removing the virtual environment name. Do you also know how to modify the prompt? (i.e change the color, or make it bold) Jun 27, 2021 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.