如何修改conda提示符字符串内容

4

我如何在不触及常规提示符的情况下编辑conda提示符的行为?我想保留conda的添加到PS1前缀的行为,但更改要添加的字符串。

与此非常相似的问题如何修改conda 'source activate' ps1行为。然而,那里的解决方法是将conda前缀注入到PS1的中间和/或编辑PROMPT_COMMAND。这破坏了我想要的封装性,并且在仍希望保留添加到PS1前缀的行为时非常笨拙。


我的正常提示字符串看起来像这样:

previous output
previous output

user@short-domain fullpath
$

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

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

我不喜欢这种做法会消除前一个命令输出和新提示之间的空行。与我上面提到的问题不同,我特别希望(<env-name-here>)单独成行:
previous output
previous output

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

注意这意味着conda提示符修改需要包括自己的换行符。我可以将其他问题的答案改为可用,但是我不想触及与我的正常提示符相关的任何变量。
1个回答

2

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

conda config命令

conda config命令本身非常有用。执行

$ conda config --describe

这段文字展示了与配置页面相同的信息。

由于我的.condarc文件不在默认位置,我使用--system选项来运行conda config,以防止conda在我的主目录中创建新的.condarc文件。从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.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接