virtualenv name not show in zsh prompt

Recently, I give a try on oh my zsh, everything looks good till I try virtualevn and virtualenvwrapper. When I activate a virtualenv (e.g test), on normal bash, I will see the virtualenv name like:

(test)abc@abc:

But when I switched to zsh, I cannot see virtualenv name. Even though, I alr add virtualenv and virtualenvwrapper in plugins of oh my zsh. I also checked the activate file of my virtualenv, it contains:

f [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then        
    _OLD_VIRTUAL_PS1="$PS1"
    if [ "x" != x ] ; then
        PS1="$PS1"
    else
        PS1="(`basename \"$VIRTUAL_ENV\"`) $PS1"
    fi
        export PS1
fi

Is it because the comparision ["x" != x] return true?

Updated: I tried to echo $PS1 in activate file, and got this:

(test) %{$fg[magenta]%}%n%{$reset_color%}%{$fg[cyan]%}@%{$reset_color%}%{$fg[yellow]%}%m%{$reset_color%}%{$fg[red]%}:%{$reset_color%}%{$fg[cyan]%}%0~%{$reset_color%}%{$fg[red]%}|%{$reset_color%}%{$fg[cyan]%}⇒%{$reset_color%}

It seems the $PS1 is correct, but when I echo $PS1 in the terminal, the (test) is gone. It seems the $PS1 is override by something else!


Solution 1:

Do this in ~/.zshrc:

plugins=(virtualenv)

POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status virtualenv)

Caveats:

1 -- add that plugin in addition to other plugins you have.

2 -- I'm using the POWERLEVEL9K theme. Maybe you theme

Solution 2:

The best solution is to add the following to the end of your ~/.zshrc file:

export VIRTUAL_ENV_DISABLE_PROMPT=

This will override the value in virtualenv.plugin.zsh - no need to change that file.

Solution 3:

My setting to display Python virtualenv name for the default (a.k.a. robbyrussell) theme is the following.

In the .zshrc file

  1. virtualenv added in plugins

  2. Add custom function:

    function virtualenv_info { 
        [ $VIRTUAL_ENV ] && echo '('`basename $VIRTUAL_ENV`') '
    }
    

Navigate to your theme

  1. My theme is the default theme for zsh:
    $ vim ~/.oh-my-zsh/themes/robbyrussell.zsh-theme
    
  2. Add this command right after existing PROMPT commands:
    PROMPT+='%{$fg[green]%}$(virtualenv_info)%{$reset_color%}%'
    

Finally

$ source ~/.zshrc

PS: You can add your name or a few space before or after the PROMPT+.

Hope that helps!

Solution 4:

Found the problem, it's due to the theme. The theme I used in the above case is pygmalion, it won't allow u to change $PS1.

After changed to robbyrussell theme, I can change $PS1 in terminal, but still cannot see the virtualenv name. After a while debugging, I found that by default the virtualenv plugin of oh my zsh disable the prompt:

# disables prompt mangling in virtual_env/bin/activate
export VIRTUAL_ENV_DISABLE_PROMPT=1

So just comment out the line in virtualenv plugin, problem solved.