Skip to content

Instantly share code, notes, and snippets.

@lexicalunit
Last active October 31, 2020 10:08
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save lexicalunit/7f8af96cb5e0db88d08d to your computer and use it in GitHub Desktop.
Save lexicalunit/7f8af96cb5e0db88d08d to your computer and use it in GitHub Desktop.
# .bashrc
################################################################################
# python environment control
################################################################################
export PYTHON_ENV=""
function entervirtualenv
{
if type virtualenvwrapper.sh >/dev/null 2>&1; then
source "$(which virtualenvwrapper.sh)"
fi
export PYTHON_ENV="virtualenv"
}
function exitvirtualenv
{
if [[ -n "$VIRTUAL_ENV" ]]; then
if type deactivate >/dev/null 2>&1; then
deactivate
fi
fi
unset PYTHON_ENV
}
function enterconda
{
# assume anaconda is installed in your home directory unless specified
if [[ -z "$1" ]]; then
export ANACONDA_ROOT="$HOME/anaconda"
else
export ANACONDA_ROOT="$1"
fi
[[ -d "$ANACONDA_ROOT/bin" ]] && PATH="$ANACONDA_ROOT/bin:$PATH"
[[ -d "$ANACONDA_ROOT/share/man" ]] && MANPATH="$ANACONDA_ROOT/share/man:$MANPATH"
export PATH
export MANPATH
export PYTHON_ENV="conda"
}
function exitconda
{
if [[ -n "$CONDA_DEFAULT_ENV" ]]; then
if which deactivate >/dev/null 2>&1; then
source deactivate
fi
fi
export PATH="$(echo "$PATH" | sed "s@$ANACONDA_ROOT/bin@@g;s@::@:@g;s@^:@@;s@:\$@@;")"
export MANPATH="$(echo "$PATH" | sed "s@$ANACONDA_ROOT/share/man@@g;s@::@:@g;s@^:@@;s@:\$@@;")"
unset PYTHON_ENV
}
################################################################################
# setup command prompt
################################################################################
function _setup_command_prompt
{
local DISPLAY_PYTHON_ENV=""
if [[ -n "$PYTHON_ENV" ]]; then
DISPLAY_PYTHON_ENV="$PYTHON_ENV"
if [[ -n "$VIRTUAL_ENV" ]]; then
DISPLAY_PYTHON_ENV="$DISPLAY_PYTHON_ENV:`basename \"$VIRTUAL_ENV\"`"
elif [[ -n "$CONDA_DEFAULT_ENV" ]]; then
DISPLAY_PYTHON_ENV="$DISPLAY_PYTHON_ENV:$CONDA_DEFAULT_ENV"
fi
DISPLAY_PYTHON_ENV="$DISPLAY_PYTHON_ENV"
fi
export PS1=... # Put $DISPLAY_PYTHON_ENV into your PS1 however you want to display it.
}
export PROMPT_COMMAND='_setup_command_prompt ' # single quotes to avoid variable expansion
@mwcraig
Copy link

mwcraig commented Jun 12, 2014

Looks excellent, thanks for sharing it!

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