zsh always show Python virtual env
Question:
I currently have this script to show my GitHub branch and virtual env:
setopt PROMPT_SUBST
autoload -Uz vcs_info
precmd() { vcs_info }
zstyle ':vcs_info:git:*' formats '(%b)'
MYPS1=''
MYPS1+='%F{green}'
MYPS1+='${${(%):-%n}:0:1}'
MYPS1+='@'
MYPS1+='${${(%):-%m}:(-4)}' # Get last 4 chars of var machine name
MYPS1+=':'
MYPS1+='%F{yellow}'
MYPS1+='%1~' # Show only the name of the working directory or ~ if it is the home directory
MYPS1+='%F{magenta}'
MYPS1+='${vcs_info_msg_0_}' # Show git branch if any
MYPS1+='%f'
MYPS1+='%# '
PS1=$MYPS1
Sometimes I need to update my .zshrc
so I run:
source ~/.zshrc
The problem is, whenever I reload my shell, I cannot see my Python virtual environment anymore even though it’s still active.
# After activating virtual env
(my-ve-3.7.13) u@m1:repo-name(github-branch)%
# After reloading my zsh
u@m1:repo-name(github-branch)%
I use pyenv
and virtualenvs
.
How can I keep the virtual env name in my prompt?
Answers:
Following @chepner’s comment, I figured it out:
- Use
env
to see the list of all env vars. pyenv
uses PYENV_VERSION
.
- Add it to the prompt, use
()
to have the same look as pyenv
does.
...
MYPS1=''
MYPS1+='($PYENV_VERSION) '
MYPS1+='%F{green}'
...
I currently have this script to show my GitHub branch and virtual env:
setopt PROMPT_SUBST
autoload -Uz vcs_info
precmd() { vcs_info }
zstyle ':vcs_info:git:*' formats '(%b)'
MYPS1=''
MYPS1+='%F{green}'
MYPS1+='${${(%):-%n}:0:1}'
MYPS1+='@'
MYPS1+='${${(%):-%m}:(-4)}' # Get last 4 chars of var machine name
MYPS1+=':'
MYPS1+='%F{yellow}'
MYPS1+='%1~' # Show only the name of the working directory or ~ if it is the home directory
MYPS1+='%F{magenta}'
MYPS1+='${vcs_info_msg_0_}' # Show git branch if any
MYPS1+='%f'
MYPS1+='%# '
PS1=$MYPS1
Sometimes I need to update my .zshrc
so I run:
source ~/.zshrc
The problem is, whenever I reload my shell, I cannot see my Python virtual environment anymore even though it’s still active.
# After activating virtual env
(my-ve-3.7.13) u@m1:repo-name(github-branch)%
# After reloading my zsh
u@m1:repo-name(github-branch)%
I use pyenv
and virtualenvs
.
How can I keep the virtual env name in my prompt?
Following @chepner’s comment, I figured it out:
- Use
env
to see the list of all env vars.pyenv
usesPYENV_VERSION
. - Add it to the prompt, use
()
to have the same look aspyenv
does.
...
MYPS1=''
MYPS1+='($PYENV_VERSION) '
MYPS1+='%F{green}'
...