@@ -10,25 +10,71 @@ an email or post a comment on `this blog post
1010<https://doughellmann.com/blog/2010/01/12/virtualenvwrapper-tips-and-tricks/> `__
1111and I'll add it here.
1212
13- zsh Prompt
14- ==========
15-
16- From Nat (was blogger.com/profile/16779944428406910187):
17-
18- Using zsh, I added some bits to ``$WORKON_HOME/post(de)activate `` to show
19- the active virtualenv on the right side of my screen instead.
20-
21- in ``postactivate ``::
22-
23- PS1="$_OLD_VIRTUAL_PS1"
24- _OLD_RPROMPT="$RPROMPT"
25- RPROMPT="%{${fg_bold[white]}%}(env: %{${fg[green]}%}`basename \"$VIRTUAL_ENV\"`%{${fg_bold[white]}%})%{${reset_color}%} $RPROMPT"
26-
27- and in ``postdeactivate ``::
13+ Enhanced bash/zsh Prompt
14+ ========================
15+
16+ Via `Stephan Sokolow <https://github.com/ssokolow/ >`_
17+
18+ While the virtualenv ``activate `` script does attempt to provide
19+ an indicator in the prompt, it has various shortcomings, and
20+ cannot be customized.
21+
22+ However, it does also set a shell variable named
23+ ``VIRTUAL_ENV `` which can be used as the basis for disabling the
24+ built-in prompt indicator and substituting an improved one,
25+ as a customization to ``.bashrc `` or ``.zshrc ``::
26+
27+ virtualenv_prompt() {
28+ # If not in a virtualenv, print nothing
29+ [[ "$VIRTUAL_ENV" == "" ]] && return
30+
31+ # Distinguish between the shell where the virtualenv was activated
32+ # and its children
33+ local venv_name="${VIRTUAL_ENV##*/}"
34+ if typeset -f deactivate >/dev/null; then
35+ echo "[${venv_name}] "
36+ else
37+ echo "<${venv_name}> "
38+ fi
39+ }
40+
41+ # Display a "we are in a virtualenv" indicator that works in child shells too
42+ VIRTUAL_ENV_DISABLE_PROMPT=1
43+ PS1='$(virtualenv_prompt)'"$PS1"
44+
45+ This basic example works in both bash and zsh and has the following
46+ advantages:
47+
48+ 1. It will also display in sub-shells, because it works by having the
49+ shell detect an active virtualenv, rather than by having the ``activate ``
50+ script modify the prompt for just the current shell instance.
51+ 2. It will clearly indicate if you're in a subshell, where the
52+ virtualenv will still apply, but the ``deactivate `` command will be
53+ missing.
54+
55+ However, if you are using zsh, a better example of what the design
56+ is capable of can be constructed by taking advantage of zsh's built-in
57+ support for easily adding color and right-aligned segments to prompts::
58+
59+ zsh_virtualenv_prompt() {
60+ # If not in a virtualenv, print nothing
61+ [[ "$VIRTUAL_ENV" == "" ]] && return
62+
63+ # Distinguish between the shell where the virtualenv was activated
64+ # and its children
65+ local venv_name="${VIRTUAL_ENV##*/}"
66+ if typeset -f deactivate >/dev/null; then
67+ echo "[%F{green}${venv_name}%f] "
68+ else
69+ echo "<%F{green}${venv_name}%f> "
70+ fi
71+ }
2872
29- RPROMPT="$_OLD_RPROMPT"
73+ setopt PROMPT_SUBST PROMPT_PERCENT
3074
31- Adjust colors according to your own personal tastes or environment.
75+ # Display a "we are in a virtualenv" indicator that works in child shells too
76+ VIRTUAL_ENV_DISABLE_PROMPT=1
77+ RPS1='$(zsh_virtualenv_prompt)'
3278
3379Updating cached ``$PATH `` entries
3480=================================
0 commit comments