-
Add the virtualenv plugin to
~/.zshrc
and make sure these lines are in~/.oh-my-zsh/plugins/virtualenv/virtualenv.plugin.zsh
# disables prompt mangling in virtual_env/bin/activate export VIRTUAL_ENV_DISABLE_PROMPT=1 #Disable conda prompt changes #https://conda.io/docs/user-guide/configuration/use-condarc.html#change-command-prompt-changeps1 #changeps1: False `conda config --set changeps1 false`
-
Add these two helper functions to
~/.oh-my-zsh/themes/bira.zsh-theme
(add them near the top, not at the end)function venv_info { if [[ -n "$VIRTUAL_ENV" ]]; then echo "%{$fg[green]%}‹${VIRTUAL_ENV:t}›%{$reset_color%}" fi } function conda_info { if [[ -n "$CONDA_DEFAULT_ENV" ]]; then echo "%{$fg[green]%}‹${CONDA_DEFAULT_ENV}›%{$reset_color%}" fi } local venv='$(venv_info)' local conda='$(conda_info)'
-
Add it to the prompt
Look for the
PROMPT
variable in the same file and add${conda} ${venv}
to it (The order in which they should appear is your preference). It should look like thisPROMPT="╭─${user_host} ${current_dir} ${rvm_ruby} ${git_branch} ${conda} ${venv} ╰─%B${user_symbol}%b "
-
Source
~/.zshrc
to see if changes have taken place
-
-
Save Samyak2/6676c608371e915e3c066dbbdcc25622 to your computer and use it in GitHub Desktop.
I don't use oh-my-zsh and bira anymore, so I can't test it currently. @akshatgurnani do you see any errors? Can you describe the issue some more?
Thanks for sharing!
I did not have to modify the virtualenv plugin. But I had to activate it in .zshrc:
plugins=(git virtualenv)
In the theme, I just have the following:
local venv_prompt='$(virtualenv_prompt_info)'
PROMPT="╭─${user_host} ${current_dir} ${rvm_ruby} ${git_branch} ${venv_prompt}
╰─%B${user_symbol}%b "
That's all it needs for it to work. The conda prefix is also removed this way, no need to modify .condarc (see this post further up)
See update below!
Update: In my previous comment, I referred to a system using pyenv, which sets the environment flag VIRTUAL_ENV when it creates an virtual environment. That's why I could simply rely on the virtualenv plugin.
Conda, however, sets different environment flag (e.g., CONDA_PREFIX, CONDA_DEFAULT_ENV, etc.). In that case, the virtualenv plugin does not work out of the box.
As pyenv can also manage conda environments (not just normal python environments), it is possible that both CONDA_DEFAULT_ENV and VIRTUAL_ENV at the same time. That's why I got two (different) virtualenv tags in the prompt when using the solution by @Samyak2.
I found a setup that works for me now:
- Enable the virtualenv plugin of oh-my-zsh in your .zshrc:
plugins=(git virtualenv)
- Modify the virutalenv plugin (~/.oh-my-zsh/plugins/virtualenv/virtualenv.plugin.zsh, see below)
- Optionally: in .condarc, set
changeps1: false
(and alsoauto_activate_base: false
, if this is what you prefer) - Adjust the Bira theme (~/.oh-my-zsh/themes/bira.zsh-theme): Replace the following line
# --> bira.zsh-theme
# ...
# Replace the following line:
# local venv_prompt='$(virtualenv_prompt_info)'
local venv_prompt='$(virtualenv_conda_prompt_info)'
# The prompt looks similar to this:
PROMPT="╭─${user_host} ${current_dir} ${rvm_ruby} ${git_branch} ${venv_prompt}
╰─%B${user_symbol}%b "
# ...
# --> virtualenv.plugin.zsh
# modified version
function virtualenv_prompt_info(){
[[ -n ${VIRTUAL_ENV} ]] || return
echo "${ZSH_THEME_VIRTUALENV_PREFIX=[}${VIRTUAL_ENV:t:gs/%/%%}${ZSH_THEME_VIRTUALENV_SUFFIX=]}"
}
# Disables prompt mangling in virtual_env/bin/activate
export VIRTUAL_ENV_DISABLE_PROMPT=1
# Also handle the presence of conda. The following works also
# if conda is part of an environment managed by pyenv.
# Note: conda does not set VIRTUAL_ENV, while pyenv does.
function virtualenv_conda_prompt_info(){
[[ -n ${VIRTUAL_ENV} || -n ${CONDA_PREFIX} ]] || return
local env_info=${VIRTUAL_ENV:-$CONDA_PREFIX}
echo "${ZSH_THEME_VIRTUALENV_PREFIX=[}${env_info:t:gs/%/%%}${ZSH_THEME_VIRTUALENV_SUFFIX=]}"
}
if [[ -n ${CONDA_EXE} ]]; then
# https://stackoverflow.com/questions/36499220/
# https://stackoverflow.com/questions/54429210/
# This modifies ~/.condarc
conda config --set changeps1 False
conda config --set auto_activate_base False
fi
I tested the following scenarios:
- native Python environment
- conda environment
- pyenv
- pyenv with virtualenv plugin
- pyenv with conda
I hope is of use to anyone.
I'm using the jonathan theme , and all the solution above didn't work in jonathan because Jonathan has a different code structure from Bira . To display conda info in jonathan , just add the function "conda_info" to .oh-my-zsh/themes/jonathan.zsh-theme
:
conda_prompt_info(){
if [ -n "$CONDA_DEFAULT_ENV" ]; then
echo -n "($CONDA_DEFAULT_ENV)"
else
echo -n "(base)"
fi
}
As Jonathan has 2 lines of output and the first output line was filled by a function using "------" . If conda info displays in the first line , those "------" will be out of your screen . So :
add '${$(conda_prompt_info)}' to the SECOND LINE OF OUTPUT in the PROMPT . the PROMPT was devided into 2 parts by a empty line , so you can easily find where to add conda info . For me , I added it after the time info . My PROMPT is like
PROMPT=\'${PR_SET_CHARSET}${PR_STITLE}${(e)PR_TITLEBAR}\
${PR_CYAN}${PR_ULCORNER}${PR_HBAR}${PR_GREY}(\
${PR_GREEN}%${PR_PWDLEN}<...<%~%<<\
${PR_GREY})$(ruby_prompt_info)${PR_CYAN}${PR_HBAR}${PR_HBAR}${(e)PR_FILLBAR}${PR_HBAR}${PR_GREY}(\
${PR_CYAN}%(!.%SROOT%s.%n)${PR_GREY}@${PR_GREEN}%m:%l\
${PR_GREY})${PR_CYAN}${PR_HBAR}${PR_URCORNER}\
${PR_CYAN}${PR_LLCORNER}${PR_BLUE}${PR_HBAR}(\
${PR_YELLOW}%D{%H:%M:%S}\
${PR_LIGHT_BLUE}%{$reset_color%}$(git_prompt_info)$(git_prompt_status)${PR_BLUE})${$(conda_prompt_info)}${venv_prompt}${PR_CYAN}${PR_HBAR}\
${PR_HBAR}\
>${PR_NO_COLOUR} \'
I hope this will help someone too XD
Not working in bira