I use zsh theme intheloops
. The theme looks like this when no virtual env. is active
-- an empty line --
[sourabh@skynet] ~/Code/django_apps/cope (master) ⚡
❯
and when an env. is active,
(env name)
[sourabh@skynet] ~/Code/django_apps/cope (master) ⚡
❯
Can I make it look like this when some virtualenv is active
-- empty line --
(env name) [sourabh@skynet] ~/Code/django_apps/cope (master) ⚡
❯
.zsh-theme
file
local return_status="%{$fg[red]%}%(?..⏎)%{$reset_color%}"
local host_color="green"
if [ -n "$SSH_CLIENT" ]; then
local host_color="red"
fi
PROMPT='
%{$fg_bold[grey]%}[%{$reset_color%}%{$fg_bold[${host_color}]%}%n@%m%{$reset_color%}%{$fg_bold[grey]%}]%{$reset_color%} %{$fg_bold[blue]%}%10c%{$reset_color%} $(git_prompt_info) $(git_remote_status)
%{$fg_bold[cyan]%}❯%{$reset_color%} '
RPROMPT='${return_status}%{$reset_color%}'
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg[yellow]%}(%{$fg[yellow]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[yellow]%}) %{$fg[pink]%}⚡%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[grey]%})"
ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE="%{$fg_bold[magenta]%}↓%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE="%{$fg_bold[magenta]%}↑%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIVERGED_REMOTE="%{$fg_bold[magenta]%}↕%{$reset_color%}"
How the prompt is changed is defined in the script
bin/activate
inside the virtual environment directory. This file is created byvirtualenv
from a template. Unfortunatelly, the only way of prompt modification provided by the template is prepending(env name)
or whatever is set with--prompt
.To modify the prompt in the way you want, I'd suggest circumventing the setting of the prompt in
bin/activate
and modify the definition ofPROMPT
in your theme file.First add the following to your
.zsh-theme
(or.zshrc
)and add
%(1V.(%1v).)
in front of the second line of the definition ofPROMPT
. It should then look like this:If you want some color you could add
%(1V.%{$fs_bold[yellow]%}(%1v)%{$reset_color%}.)
for example.Explanation:
virtenv_indicator
will be called each time before the prompt is created. It checks if$VIRTUAL_ENV
is set and not empty. If so, it sets the first element of the$psvar
array to$VIRTUAL_ENV
with everything before and including the last/
removed (likebasename $VIRTUAL_ENV
but less expensive)In the definition of
PROMPT
%(1V.(%1v).)
checks if the first element of$psvar
is set and not empty (%(1V.true-text.false-text)
) and adds the content of the this element plus some parentheses ((%1v)
)export VIRTUAL_ENV_DISABLE_PROMPT=yes
disables any prompt setting bybin/activate
scripts.Oh-my-zsh
now includes avirtualenv
plugin, so just enable it in theconfig
.If you're using prezto, you can find instructions on how to display the active virtualenv here: https://github.com/sorin-ionescu/prezto/tree/master/modules/python#theming