Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adding virtualenv and conda support to Oh My Zsh Bira theme

Steps to add virtualenv and conda support to Bira theme

  • 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 this

    PROMPT="╭─${user_host} ${current_dir} ${rvm_ruby} ${git_branch} ${conda} ${venv}
    ╰─%B${user_symbol}%b "
  • Source ~/.zshrc to see if changes have taken place

@jerlich

This comment has been minimized.

Copy link

@jerlich jerlich commented Jun 18, 2020

nice!

@zakiaziz

This comment has been minimized.

Copy link

@zakiaziz zakiaziz commented May 3, 2021

does this work with other themes?

@Samyak2

This comment has been minimized.

Copy link
Owner Author

@Samyak2 Samyak2 commented May 3, 2021

I'm not using bira currently, so I'm not very sure, but it should work with others themes. Try it out and see if it works? Let me know if it does (or doesn't)

@zakiaziz

This comment has been minimized.

Copy link

@zakiaziz zakiaziz commented May 5, 2021

I only wanted conda info and the colors stripped so I've modifed the code. This works:

function conda_info() {
    if [[ -n "$CONDA_DEFAULT_ENV" ]]; then
        echo "${CONDA_DEFAULT_ENV}"
    fi
}

I haven't tried this but if anyone needs venv env info this should work:

function venv_info() {
    if [[ -n "$VIRTUAL_ENV" ]]; then
        echo "${VIRTUAL_ENV:t}"
    fi
}
@Samyak2

This comment has been minimized.

Copy link
Owner Author

@Samyak2 Samyak2 commented May 5, 2021

Thank you for sharing!

@dimakis

This comment has been minimized.

Copy link

@dimakis dimakis commented Sep 28, 2021

Very helpful, this solution also works for 'strug' theme. Thank you very much for sharing!

@Samyak2

This comment has been minimized.

Copy link
Owner Author

@Samyak2 Samyak2 commented Sep 29, 2021

Good to know!

@Zepeng-Mu

This comment has been minimized.

Copy link

@Zepeng-Mu Zepeng-Mu commented Oct 27, 2021

when I activate a conda environment, the conda env name or prefix is shown at the very beginning of prompt, and this is not part of the PROMPT in the theme file, so I'm getting two conda names, I'd like to get rid of the first one though. For example:

(base) ╭─user@x86_64-conda-linux-gnu ~ ‹base›
╰─$ conda deactivate
@Zepeng-Mu

This comment has been minimized.

Copy link

@Zepeng-Mu Zepeng-Mu commented Oct 27, 2021

Well, I set env_prompt: "" in .condarc and now things are working as expected:

╭─user@x86_64-conda-linux-gnu ~ ‹base›
╰─$

Thanks for this snippet!

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