bash 如何修改 conda 'source activate' ps1 行为

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42481726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:49:35  来源:igfitidea点击:

how to modify conda 'source activate' ps1 behavior

bashconda.bash-profileps1

提问by jkarimi

my current bash ps1 is as follows:

我目前的 bash ps1 如下:

bldred='\e[1;31m' # Red
bldcyn='\e[1;36m' # Cyan
bldwht='\e[1;37m' # White
txtrst='\e[0m'    # Text Reset - Useful for avoiding color bleed

export PS1="\n\[$bldred\]\u\[$txtrst\]@\[$bldwht\]\h\[$txtrst\]:\[$bldcyn\]\w\[$txtrst\]$ "

However, running:

但是,运行:

source activate <env-name-here>

by default, tells condato prepend the env-nameto my PS1:

默认情况下,告诉condaenv-namemy 之前添加PS1

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

Is there a way to tell condato insert the env-namewithin my PS1instead, specifically, right after the newline?

有没有办法告诉在我condaenv-name内部插入PS1,特别是在换行符之后?

采纳答案by jkarimi

The simplest solution I have found is to move the newline from PS1to PROMPT_COMMAND:

我发现的最简单的解决方案是将换行符从PS1to移动PROMPT_COMMAND

bldred='\e[1;31m' # Red
bldcyn='\e[1;36m' # Cyan
bldwht='\e[1;37m' # White
txtrst='\e[0m'    # Text Reset - Useful for avoiding color bleed

PROMPT_COMMAND="printf '\n'"
export PS1="\[$bldred\]\u\[$txtrst\]@\[$bldwht\]\h\[$txtrst\]:\[$bldcyn\]\w\[$txtrst\]$ "

This allows condato maintain it's default PS1behavior all while separating bash commands with newlines:

这允许condaPS1使用换行符分隔 bash 命令的同时保持其默认行为:

user@short-domain:fullpath$ source activate <env-name-here>

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

回答by remram

Conda has a setting to disable changing the prompt: changeps1: False(in ~/.condarc). You can then add this yourself ($(basename "$CONDA_PREFIX")).

Conda 有一个禁止更改提示的设置:changeps1: False(in ~/.condarc)。然后您可以自己添加它 ( $(basename "$CONDA_PREFIX"))。

This is similar to virtualenv, which doesn't update the prompt if $VIRTUAL_ENV_DISABLE_PROMPTis set, so you can print $(basename "$VIRTUAL_ENV")yourself.

这类似于virtualenv,如果$VIRTUAL_ENV_DISABLE_PROMPT设置了则不会更新提示,因此您可以$(basename "$VIRTUAL_ENV")自己打印。