how to modify conda 'source activate' ps1 behavior
Asked Answered
E

2

20

my current bash ps1 is as follows:

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 conda to prepend the env-name to my PS1:

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

Is there a way to tell conda to insert the env-name within my PS1 instead, specifically, right after the newline?

Electric answered 27/2, 2017 at 9:12 Comment(3)
I don't know conda at all, but couldn't you just pass it the string <env-name-here>\n and remove the \n from your PS1?Meter
@Meter I explain how to do exactly what you described here: https://mcmap.net/q/282301/-how-to-modify-conda-prompt-string-content. jkarimi seems to be asking how to keep <env-name-here> on the same line as the rest of his prompt string, so adding a trailing newline to <env-name-here> would defeat the point.Constantina
Beside the point, but you don't need to export PS1Vazquez
E
13

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

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

This allows conda to maintain it's default PS1 behavior all while separating bash commands with newlines:

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

(<env-name-here>) user@short-domain:fullpath$
Electric answered 27/2, 2017 at 10:11 Comment(0)
S
19

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

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

Spontaneity answered 30/3, 2018 at 21:24 Comment(2)
instead of the "CONDA_PREFIX", I used the "CONDA_PROMPT_MODIFIER" in my PS1 for a smoother integration.Farthermost
@Farthermost if you don't need to insert CONDA_PROMPT_MODIFIER into the middle of PS1, (i.e. prepending is still okay) the Q&A I posted is even cleaner: https://mcmap.net/q/282301/-how-to-modify-conda-prompt-string-contentConstantina
E
13

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

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

This allows conda to maintain it's default PS1 behavior all while separating bash commands with newlines:

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

(<env-name-here>) user@short-domain:fullpath$
Electric answered 27/2, 2017 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.