11

I created an anaconda environment in a project folder specifying the path with -p option, i.e. not in the default anaconda3/envs folder:

conda create -p venv

The problem is that when I activate that environment, the bash prefix in the terminal is too long, i.e. it prepends the entire path of the environment to the prompt:

(/path/to/the/environment/venv) user@machine: ~/path/to/environment/$

Is there a way to fix this, meaning make it shorter or delete the prefix from prompt?

My $PS1:

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Dark Templar
  • 1,175
  • 13
  • 27

1 Answers1

22

Conda Prompt Customization

Since Conda v4.6.0 there has been the env_prompt configuration option to provide for customization of the PS1 change. Here is the description:

$ conda config --describe env_prompt
# # env_prompt (str)
# #   Template for prompt modification based on the active environment.
# #   Currently supported template variables are '{prefix}', '{name}', and
# #   '{default_env}'. '{prefix}' is the absolute path to the active
# #   environment. '{name}' is the basename of the active environment
# #   prefix. '{default_env}' holds the value of '{name}' if the active
# #   environment is a conda named environment ('-n' flag), or otherwise
# #   holds the value of '{prefix}'. Templating uses python's str.format()
# #   method.
# # 
# env_prompt: '({default_env}) '

One option that would help with your case would be to just use the {name} variable

conda config --set env_prompt '({name}) '

which will show only the env's folder name. E.g., your example would be

(venv) user@machine: ~/path/to/environment/$

Note, this will make it so that when the base env is active the prompt will show (anaconda3) instead of (base); otherwise, the other named envs should appear as usual.

If you really can't stand that, you could run basename {default_env} to get the same result as {name} on unnamed envs and still retain base. That is,

conda config --set env_prompt '(\$(basename {default_env})) '
merv
  • 67,214
  • 13
  • 180
  • 245
  • 2
    Thank you! All of this works for me but with a small correction: ```conda config --set env_prompt '($(basename {default_env})) ' ``` – JDS Jun 18 '21 at 13:58
  • I want to only display the parent folder name of the environment, so for prefix "/Users/username/code/llama.cpp/.env", I would like to see "llama.cpp" (i.e. the git repo name), and for named conda envs I want to see the name as usual. From you solution I tried to set `conda config --set env_prompt '($(basename $(dirname {prefix}))) '` but it doesn't work. Any ideas on how to achieve this? Running conda 23.3.1 on macOS, zsh shell. – artofbeinghuman Jun 14 '23 at 14:05