19

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?

jkarimi
  • 1,247
  • 2
  • 15
  • 27
  • I don't know `conda` at all, but couldn't you just pass it the string `\n` and remove the `\n` from your PS1? – Aaron Feb 27 '17 at 09:26
  • @Aaron I explain how to do exactly what you described here: https://stackoverflow.com/q/62842563. jkarimi seems to be asking how to keep `` on the same line as the rest of his prompt string, so adding a trailing newline to `` would defeat the point. – drmuelr Jul 10 '20 at 21:35
  • Beside the point, but [you don't need to `export PS1`](https://unix.stackexchange.com/q/247585/117037) – wjandrea Oct 03 '20 at 16:04

2 Answers2

17

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.

remram
  • 4,805
  • 1
  • 29
  • 42
  • 6
    instead of the "CONDA_PREFIX", I used the "CONDA_PROMPT_MODIFIER" in my PS1 for a smoother integration. – Adriano May 09 '19 at 09:25
  • @Adriano 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://stackoverflow.com/q/62842563 – drmuelr Jul 10 '20 at 21:40
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$
wjandrea
  • 28,235
  • 9
  • 60
  • 81
jkarimi
  • 1,247
  • 2
  • 15
  • 27