How to modify conda prompt string content

痴心易碎 提交于 2020-07-23 06:48:27

问题


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.


回答1:


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.


来源:https://stackoverflow.com/questions/62842563/how-to-modify-conda-prompt-string-content

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!