By default, when I enter a pipenv shell, the start of my prompt indicates the workspace in which I'm working.
user@user-dsktp:~/main$ pipenv shell
Spawning environment shell (/bin/bash). Use 'exit' to leave.
user@user-dsktp:~/main$ . /home/user/.local/share/virtualenvs/main-GqtUWnwk/bin/activate
(main-GqtUWnwk) user@user-dsktp:~/main$
However, if I customise my prompt (to display git information):
PROMPT_COMMAND='__git_ps1 "\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]" "\n\\\$ "'
I lose the workspace information:
user@user-dsktp:~/main (develop=)
$ pipenv shell
Spawning environment shell (/bin/bash). Use 'exit' to leave.
user@user-dsktp:~/main (develop=)
$ . /home/user/.local/share/virtualenvs/main-GqtUWnwk/bin/activate
user@user-dsktp:~/main (develop=)
$
Does anyone know how to get it back? Any help much appreciated.
From the GNU Bash Reference Manual:
If set, the value is interpreted as a command to execute before the printing of each primary prompt (
$PS1
).
In your case, you run a function __git_ps1
with parameter "\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]" "\n\\\$ "
before $PS1
is print.
What is __git_ps1
? That depends on your environment (it is not a standard Bash function), but it is a help function that produces a PS1 with Git information. You can see what it is with
$ type __git_ps1
If you look into the source, you may find __git_ps1
overwrites the default PS1. This is why the virtual environment name is not shown. The virtual environment’s name, as @chepner mentioned, is displayed via the activate
script by modifying your PS1
. __git_ps1
, however, overwrites that change.
The way to fix it once and for all is to learn some Bash, and try to craft a better PROMPT_COMMAND
:) Or you can use this instead
export PROMPT_COMMAND='
__PS1_SUFFIX="\\\$ "
if [[ -n "$VIRTUAL_ENV" ]]; then
__PS1_SUFFIX="($(basename "$VIRTUAL_ENV")) $__PS1_SUFFIX"
fi
__git_ps1 "\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]" "\n$__PS1_SUFFIX"
'
This snippet checks if VIRTUAL_ENV
is set (this would point to the virtual environment’s root if set by virtualenv), and adds its name to the second line of the prompt (befure $
). Now this is not the best way to do it because the directory name does not always match the prompt, but it should work for you right now. You can decide yourself whether to descend deeper into this Bash script abyss if you ever find this snippet insufficient for your use case.