Skip to content

Instantly share code, notes, and snippets.

@frnhr
Created March 17, 2016 22:39
Show Gist options
  • Save frnhr/dba7261bcb6970cf6121 to your computer and use it in GitHub Desktop.
Save frnhr/dba7261bcb6970cf6121 to your computer and use it in GitHub Desktop.
Show current pyenv python version in bash prompt, and also color virtual envs differently
####
#### pyenv-virtualenv bash prompt customization
####
# pyenv
eval "$(pyenv init -)"
# pyenv-virtualenv:
eval "$(pyenv virtualenv-init -)"
export PYENV_VIRTUALENV_DISABLE_PROMPT=1
pyenvVirtualenvUpdatePrompt() {
RED='\[\e[0;31m\]'
GREEN='\[\e[0;32m\]'
BLUE='\[\e[0;34m\]'
RESET='\[\e[0m\]'
[ -z "$PYENV_VIRTUALENV_ORIGINAL_PS1" ] && export PYENV_VIRTUALENV_ORIGINAL_PS1="$PS1"
[ -z "$PYENV_VIRTUALENV_GLOBAL_NAME" ] && export PYENV_VIRTUALENV_GLOBAL_NAME="$(pyenv global)"
VENV_NAME="$(pyenv version-name)"
VENV_NAME="${VENV_NAME##*/}"
GLOBAL_NAME="$PYENV_VIRTUALENV_GLOBAL_NAME"
# non-global versions:
COLOR="$BLUE"
# global version:
[ "$VENV_NAME" == "$GLOBAL_NAME" ] && COLOR="$RED"
# virtual envs:
[ "${VIRTUAL_ENV##*/}" == "$VENV_NAME" ] && COLOR="$GREEN"
if [ -z "$COLOR" ]; then
PS1="$PYENV_VIRTUALENV_ORIGINAL_PS1"
else
PS1="($COLOR${VENV_NAME}$RESET)$PYENV_VIRTUALENV_ORIGINAL_PS1"
fi
export PS1
}
export PROMPT_COMMAND="$PROMPT_COMMAND pyenvVirtualenvUpdatePrompt;"
## Recommanded pyenv setup:
## run this once, don't put it in .bash_profile (or similar) files!
# $ pyenv global system
# $ cd $HOME
# $ pyenv local 2.7.11 # or which ever version you like having as default
@itsthejoker
Copy link

Thank you so much for this. I held on as long as I could until they finally killed the feature officially, but I spend so much time bouncing between environments that this is essential. I really appreciate it!

@wenbochang
Copy link

thanks sir!

@JamesHh666
Copy link

thank you so much!

@changchichung
Copy link

may I ask how to update this script to do not modify prompt when using the system python??

@jcmpes
Copy link

jcmpes commented Feb 17, 2022

Hallellullay! Thank you very much

@frnhr
Copy link
Author

frnhr commented Feb 17, 2022

@changchichung Almost a year later (I failed to notice the question, sorry), the answer is to just change this line:

    [ "$VENV_NAME" == "$GLOBAL_NAME" ] && COLOR="$RED"

into:

    [ "$VENV_NAME" == "$GLOBAL_NAME" ] && COLOR=""

This will make the hook do nothing when using the default python env.

@arcsector
Copy link

arcsector commented May 25, 2022

For people coming to this using fish, I somewhat replicated this functionality with this in my config.fish:

# pyenv init
if command -v pyenv 1>/dev/null 2>&1
  pyenv init - | source
end

set -gx PYENV_VIRTUALENV_DISABLE_PROMPT 1

function pyenvVirtualenvUpdatePrompt
    set RED 'red'
    set GREEN 'green'
    set BLUE 'blue'
    set RESET 'normal'
    [ -z "$PYENV_VIRTUALENV_ORIGINAL_PS1" ] && set -x PYENV_VIRTUALENV_ORIGINAL_PS1 "$PS1"
    [ -z "$PYENV_VIRTUALENV_GLOBAL_NAME" ] && set -x PYENV_VIRTUALENV_GLOBAL_NAME "(pyenv global)"
    set VENV_NAME (pyenv version-name)
    set VENV_NAME "(echo $VENV_NAME | sed 's:/*\$::')"
    set GLOBAL_NAME "$PYENV_VIRTUALENV_GLOBAL_NAME"

    # non-global versions:
    set COLOR "$BLUE"
    # global version:
    test "$VENV_NAME" = "$GLOBAL_NAME" && set COLOR "$RED"
    # virtual envs:
    test "(echo $VIRTUAL_ENV | sed 's:/*\$::')" = "$VENV_NAME" && set COLOR "$GREEN"
    set_color $COLOR; echo (pyenv version-name)
end

Note that I'm using this guide for fish installation of pyenv, so no guarantee this will work with Oh My Fish. Next you edit your fish prompt with funced fish_prompt and put this:

fish_prompt> function fish_prompt --description 'Write out the prompt'
                 .......
                 echo -n -s (prompt_login)' ' (set_color $color_cwd) (prompt_pwd) $normal (fish_vcs_prompt) $normal " " (pyenvVirtualenvUpdatePrompt) $normal " "$prompt_status $suffix " "
             end

NOTE: You may have to put this whole function in ~/.config/fish/functions/fish_prompt.fish in order to make it persist across reboots.

The key is putting in the space after the VCS prompt, followed by the function we initialized, and then the $normal color for whatever comes after it in fish. After a refresh, with pyenv local 3.6.15 your prompt should look like:

user@srv-1001 /m/c/U/h/D/s/G/tls (oop) 3.6.15>

Hope this helps everyone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment