Skip to content

Instantly share code, notes, and snippets.

@abaksy
Last active May 7, 2025 12:07
Show Gist options
  • Save abaksy/52d6e6d84087d4e32ad285b74f85c9eb to your computer and use it in GitHub Desktop.
Save abaksy/52d6e6d84087d4e32ad285b74f85c9eb to your computer and use it in GitHub Desktop.
Displaying virtualenv and conda information on the Oh-My-Zsh Agnoster theme
  • Add the virtualenv plugin in the ~/.zshrc file. This is done by modifying the plugins line to include the virtualenv plugin
  • Make sure the following lines are in the file ~/.oh-my-zsh/plugins/virtualenv/virtualenv.plugin.zsh
# disables prompt mangling in virtual_env/bin/activate
export VIRTUAL_ENV_DISABLE_PROMPT=1

#Disable conda prompt changes
#https://conda.io/docs/user-guide/configuration/use-condarc.html#change-command-prompt-changeps1
#changeps1: False
`conda config --set changeps1 false`
  • In the file ~/.oh-my-zsh/themes/agnoster.zsh-theme locate the function called prompt_virtualenv() and modify it to look as follows:
# Display current virtual environment
prompt_virtualenv() {
  local env='';

  # if "$CONDA_DEFAULT_ENV" variable exists,
  # then you are using conda to manage python virtual env
  if [[ -n "$CONDA_DEFAULT_ENV" ]]; then
    env="$CONDA_DEFAULT_ENV"
  elif [[ -n "$VIRTUAL_ENV" ]]; then
    env="$VIRTUAL_ENV"
  fi

  if [[ -n $env ]]; then
    color=cyan
    prompt_segment $color $PRIMARY_FG
    print -Pn " $(basename $env) "
  fi
}

This function uses a conditional statement to check the type of virtual environment being used (conda or virtualenv) and grabs the name of the environment from the appropriate shell variable.
You can change the line color=cyan to whatever you like (do make sure to take a look at oh-my-zsh documentation to see what colours are allowed), and the virtual env name will be displayed in a segment of that colour. (magenta is my personal favourite :p)

  • Save the file, and run source ~/.zshrc (or restart the terminal) to see the changes
@famosab
Copy link

famosab commented Nov 29, 2023

Adding the conda env name to agnoster worked well for me! However, for removing (<env name>) I found that I had to do the following:

vim ~/.condarc

and then simply add the line

changeps1: False

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment