如何修改conda提示字符串内容 [英] How to modify conda prompt string content

查看:175
本文介绍了如何修改conda提示字符串内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不触摸正常提示的情况下编辑conda提示的行为?我想保留conda的pre-end-to-PS1行为,但是更改要添加的字符串。


问题如何修改conda源激活 ps1行为非常相似。但是,解决方案是将conda前缀注入 PS1 的中间和/或编辑 PROMPT_COMMAND 。这样会破坏我想要的封装,并且当仍然需要prepend-to-PS1行为时,这非常hacky。




我的常规提示字符串如下所示:

 上一个输出
上一个输出

user @ short-domain fullpath
$

当没有conda环境处于活动状态时,这就是我想要的行为。在活动的conda环境中,它变为:

 上一个输出
上一个输出
(< env-name-here> )
用户@短域全路径
$

我不喜欢这样消除了前一条命令的输出和新提示之间的空白行。与我上面提到的问题不同,我特别希望(< env-name-here>)自己行:

 上一个输出
上一个输出

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

请注意,这意味着conda提示修改需要包含其自己的换行符。我可以破解另一个问题的答案,以解决问题,但我又不想触碰任何与常规提示相关的变量。

解决方案

使用conda的本机
配置选项有一种正确而简单的方法。


编辑 .condarc 文件包含以下行:

  env_prompt:\n({default_env})

或运行命令:

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

这两个都将为新终端带来理想的效果。
请注意,-system 选项对于许多用例可能不是理想的。
有关更多详细信息,请参见以下说明。




来自 conda文档


如果您不知道在哪里看,此功能可能难以捉摸。找到它的最自然的方法是从配置部分 conda用户指南的


使用.condarc conda配置文件 概述告诉
我们:


conda配置文件 .condarc 是可选的运行时
配置文件,允许高级用户配置conda的
各个方面,例如它搜索包的渠道,
代理设置和环境目录。有关conda
的所有配置选项,请参见配置页面


配置页面描述了所需的设置以及
的默认值:

 #env_prompt(str)
#用于根据活动环境进行即时修改的模板。
#当前受支持的模板变量是 {prefix}, {name}和
#‘{default_env}。 {prefix}是活动的
#环境的绝对路径。 {name}是活动环境
#前缀的基本名称。如果活动的
#环境是一个名为conda的环境('-n'标志),则'{default_env}'保留'{name}'的值,否则
#保留'{prefix }'。模板使用python的str.format()
#方法。

env_prompt:'({default_env})'




conda配置 命令


conda config 命令本身非常有用。

  $ conda config-描述

显示与配置页面相同的信息。 / p>

由于我的 .condarc 文件位于非默认位置,因此我使用-system 选项
用于 conda配置,以防止conda在其中创建新的 .condarc 文件我的
主目录。从 conda config --help

 配置文件位置选择:
如果没有这些标志之一,则使用位于'$ HOME / .condarc'的用户配置文件。

--system在
’< my-anaconda-install-path> /。condarc’处写入系统.condarc文件。
--env写入活动的conda环境.condarc文件
(< current-env-path>)。如果没有
环境处于活动状态,请写入用户配置文件
($ HOME / .condarc)。
--file文件写入给定文件。


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.

解决方案

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.

这篇关于如何修改conda提示字符串内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆