0

Currently, my bash prompt prints a new line after the output of the previous command via a '\n' in my PS1 value. Here is a look at my ~/.bash_profile that gets loaded when I enter the shell:

function nonzero_return() {
    local RETVAL=$?
    [[ $RETVAL -ne 0 ]] && echo "❌($RETVAL)" || echo "✅"
}

PS1='\[\e]0;Git Bash: $MSYSTEM\007\]\n\[\e[31m\]`nonzero_return`\[\e[32m\] \u@\h \[\e[35m\]\w\[\e[36m\]`__git_ps1`\e[0m\n$'

and my prompt looks like this:

✅ User@PC ~/Git
$

✅ User@PC ~/Git
$

However, when I activate a Python Virtual Environment my prompt will look like this:

(venv)
✅ User@PC ~/Git
$
(venv)
✅ User@PC ~/Git
$

I would like to maybe create a function that checks the PS1 value to see if a Virtual Environment is active (maybe by evaluating PS1 to see if it starts with a '(' ) and, if it does, add a '\n' to the beggining of PS1 so that the prompt would then look like this:

(venv)
✅ User@PC ~/Git
$

(venv)
✅ User@PC ~/Git
$

I know I could probably add to the Python activate script but I'd like the shell to do it interactively for me.

oguz ismail
  • 1
  • 16
  • 47
  • 69
Net-worker
  • 23
  • 6
  • https://stackoverflow.com/questions/10406926/how-do-i-change-the-default-virtualenv-prompt – KamilCuk Aug 02 '22 at 16:37
  • The code used by virtual environments to inject the venv name is trivial: `PS1="(venv) $PS1"`. It doesn't know or care about the escape sequence used to set your title bar, or that printing a newline before the prompt is supposed to separate the entire prompt from the preceding output. I would recommend using the `prompt_command` hook to define your prompt "from scratch" rather than trying to embed commands in a static prompt anyway. – chepner Aug 02 '22 at 16:47

0 Answers0