How to modify conda prompt string content
Asked Answered
B

1

5

How can I edit the conda prompt's behavior without touching my normal prompt? I want to retain conda's prepend-to-PS1 behavior, but change the string that gets prepended.

The question how to modify conda 'source activate' ps1 behavior is very similar. However, the solution there is to either inject the conda prefix into the middle of PS1 and/or edit PROMPT_COMMAND. This breaks the encapsulation I want, and is very hacky when the prepend-to-PS1 behavior is still desirable.


My normal prompt string looks like this:

previous output
previous output

user@short-domain fullpath
$

This is the behavior I want when no conda environment is active. With a conda environment active, this becomes:

previous output
previous output
(<env-name-here>)
user@short-domain fullpath
$

I don't like how this eliminates the blank line between the previous command's output and the new prompt. Unlike the question I mentioned above, I specifically want (<env-name-here>) on its own line:

previous output
previous output

(<env-name-here>)
user@short-domain fullpath
$

Note how this means the conda prompt modification needs to include its own newline character. I could hack the other question's answers into working, but again, I don't want to touch any variables related to my normal prompt.

Booted answered 10/7, 2020 at 21:29 Comment(0)
B
5

There is a proper and very simple way to do this using conda's native configuration options.

Edit the .condarc file to contain the line:

env_prompt: \n({default_env})

or run the command:

$ conda config --system --set env_prompt "\n({default_env})"

Either of these will achieve the desired effect for new terminals. Note that the --system option may not be desirable for many use cases. See the explanation below for more details.


From the conda documentation

This feature can be elusive if you don't know where to look. The most natural way to find it is to start with the configuration section of the conda user guide.

The "using the .condarc conda configuration file" overview tells us:

The conda configuration file, .condarc, is an optional runtime configuration file that allows advanced users to configure various aspects of conda, such as which channels it searches for packages, proxy settings, and environment directories. For all of the conda configuration options, see the configuration page.

The configuration page describes the setting we want, along with its default value:

# 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}) '

The conda config command

The conda config command is quite useful on its own. Doing

$ conda config --describe

shows the same information as the configuration page.

Since my .condarc file is in a non-default location, I use the --system option for conda config to prevent conda from creating a new .condarc file in my home directory. From conda config --help:

Config File Location Selection:
  Without one of these flags, the user config file at '$HOME/.condarc' is used.

  --system              Write to the system .condarc file at
                        '<my-anaconda-install-path>/.condarc'.
  --env                 Write to the active conda environment .condarc file
                        (<current-env-path>). If no
                        environment is active, write to the user config file
                        ($HOME/.condarc).
  --file FILE           Write to the given file.
Booted answered 10/7, 2020 at 21:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.