2

I use the official Git Bash prompt support for displaying the current branch as part of the prompt.

My problem is that activating a Python virtual environment (python -m venv <dir>) using source bin/activate doesn't display the virtual environment name - (atlassian-watchdog) - as part of the Bash prompt:

nlykkei:~/projects/atlassian-watchdog (master *)$

I've a strong feeling that it's failing, because I use PROMPT_COMMAND in ~/.bashrc instead of PS1, but GIT_PS1_SHOWCOLORHINTS works only with PROMPT_COMMAND.

Is there any way to use PROMPT_COMMAND with Python virtual environments, so that the environment name gets added to the prompt, when it's active?

~/.git-prompt.sh:

# If you would like a colored hint about the current dirty state, set
# GIT_PS1_SHOWCOLORHINTS to a nonempty value. The colors are based on
# the colored output of "git status -sb" and are available only when
# using __git_ps1 for PROMPT_COMMAND or precmd.

bin/activate:

if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
    _OLD_VIRTUAL_PS1="${PS1:-}"
    if [ "x(atlassian-watchdog) " != x ] ; then
       PS1="(atlassian-watchdog) ${PS1:-}"
    else
    if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
        # special case for Aspen magic directories
        # see http://www.zetadev.com/software/aspen/
        PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1"
    else
        PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1"
    fi
    fi
    export PS1
fi

~/.bashrc:

# git prompt
source ~/.git-prompt.sh
GIT_PS1_SHOWCOLORHINTS=1
PROMPT_COMMAND='__git_ps1 "\u:\w" "\\\$ "'
2

1 Answer 1

4

The solution is to combine the relevant parts of bin/activate with ~/git-prompt.sh.

Here __git_ps1_venv() is a function that wraps __git_ps1() and modifies the first argument of __git_ps1() to include the virtual environment.

The PS1 variable should be set to the format of your prompt.

~/.bashrc:

##################
# Prompt
##################

__git_ps1_venv() {
   local pre="$1"
   local post="$2"

   if [ -n "${VIRTUAL_ENV}" ] && [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ]; then
      if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
         # special case for Aspen magic directories
         # see http://www.zetadev.com/software/aspen/
         pre="[`basename \`dirname \"$VIRTUAL_ENV\"\``] ${pre}"
      else
         pre="(`basename \"$VIRTUAL_ENV\"`) ${pre}"
      fi
   fi

   __git_ps1 "${pre}" "${post}"
}

PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ '

if [[ -r ~/.git-prompt.sh ]]; then
   . ~/.git-prompt.sh

   GIT_PS1_SHOWCOLORHINTS=1
   GIT_PS1_SHOWDIRTYSTATE=1
   GIT_PS1_SHOWSTASHSTATE=1
   GIT_PS1_SHOWUNTRACKEDFILES=1
   GIT_PS1_SHOWUPSTREAM="verbose name"

   PROMPT_COMMAND='__git_ps1_venv "'"${PS1%\\\$ }"'" "\\\$ "'
fi
1
  • 1
    Thank you, this should be the accepted answer, and this update should be merged into git's git-prompt.sh. Sep 24, 2020 at 12:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .