Edit ZSH theme for virtualenv name

6,642

Solution 1

How the prompt is changed is defined in the script bin/activate inside the virtual environment directory. This file is created by virtualenv 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 of PROMPT in your theme file.

First add the following to your.zsh-theme (or .zshrc)

export VIRTUAL_ENV_DISABLE_PROMPT=yes

function virtenv_indicator {
    if [[ -z $VIRTUAL_ENV ]] then
        psvar[1]=''
    else
        psvar[1]=${VIRTUAL_ENV##*/}
    fi
}

add-zsh-hook precmd virtenv_indicator

and add %(1V.(%1v).) in front of the second line of the definition of PROMPT. It should then look like this:

PROMPT='
%(1V.(%1v).)%{$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%} '

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 (like basename $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 by bin/activate scripts.

Solution 2

Oh-my-zsh now includes a virtualenv plugin, so just enable it in the config.

Share:
6,642

Related videos on Youtube

Sourabh
Author by

Sourabh

Updated on September 18, 2022

Comments

  • Sourabh
    Sourabh over 1 year

    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%}"