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" "\\\$ "'
I just bumped into the same thing. Until git
and venv
work well together natively, consider including the part from env/bin/activate
that sets the prompt in the PROMPT_COMMAND
as a new function. Here's one way of doing that. See the comments for more info.
~/.bashrc :
# [...]
# Source in the original git-prompt.sh as necessary.
. ~/.git-prompt.sh
# Source a new function to be used as the prompt command (see below).
. ~/.git_ps1_venv
# Set whatever features you want.
GIT_PS1_SHOWCOLORHINTS=1
# I choose to have my original PS1 in a separate variable to keep the
# PROMPT_COMMAND line readable, and to be able to query it if needed.
ORIG_PS1='\u:\w'
# Call the new function (see below).
PROMPT_COMMAND='__git_ps1_venv "'"${ORIG_PS1}"'" "\\\$ "'
# [...]
~/.git_ps1_venv :
#!/usr/bin/env bash
__git_ps1_venv() {
# Some local storage to not clutter the environment
local pre=$1
local post=$2
# Let's only check for a virtual environment if the VIRTUAL_ENV variable is
# set. This should eek out a little more performance when we're not in one
# since we won't need to call basename.
if [ -n "${VIRTUAL_ENV}" ] && [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ]; then
# There's no need to set _OLD_VIRTUAL_PS1 since it's used to replace PS1 on
# a deactivate call. And since we're using PROMPT_COMMAND, PS1 isn't used.
#
#_OLD_VIRTUAL_PS1="${pre:-}"
# The python venv module hard-codes the name of the virtual environment into
# the activate script for my configuration, so we need to pull it out of
# VIRTUAL_ENV to have an appropriate prefix. If we're doing that, might has
# well comment out the hard-coded part and rely on the else in the
# if-statement that follows it.
#
#if [ "x(env) " != x ] ; then
# PS1="(env) ${PS1:-}"
#else
# This is the else of the if-statement with PS1 replaced with pre.
# Otherwise, no changes.
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
# Call the actual __git_ps1 function with the modified arguments
__git_ps1 "${pre}" "${post}"
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.