Last active
July 13, 2022 12:40
-
-
Save adhadse/f5755c627ea98bad19df53ee03be8689 to your computer and use it in GitHub Desktop.
A custom prompt for bash that sets python virtual environment, conda venv and git branch name on prompt (with colorful text)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Custom Prompt | |
# The various escape codes that we can use to color our prompt. | |
RED="\[\033[0;31m\]" | |
YELLOW="\[\033[1;33m\]" | |
GREEN="\[\033[0;32m\]" | |
USERNAME="\[\033[0;35m\]" | |
DIRCOLOR="\[\033[0;38;5;50m\]" | |
BLUE="\[\033[1;096m\]" | |
LIGHT_RED="\[\033[1;31m\]" | |
LIGHT_GREEN="\[\033[1;32m\]" | |
WHITE="\[\033[1;37m\]" | |
LIGHT_GRAY="\[\033[0;37m\]" | |
COLOR_NONE="\[\e[0m\]" | |
# determine git branch name | |
function parse_git_branch(){ | |
git branch --show-current 2> /dev/null | awk '{print $1}' | |
} | |
# determine mercurial branch name | |
function parse_hg_branch(){ | |
hg branch 2> /dev/null | awk '{print $1}' | |
} | |
# Determine the branch/state information for this git repository. | |
function set_git_branch() { | |
# Get and set the name of the branch. | |
git_branch=$(parse_git_branch) | |
hg_branch=$(parse_hg_branch) | |
# if not git then maybe mercurial | |
if [ "$git_branch" != "" ]; then | |
BRANCH=" ${LIGHT_GREEN}${git_branch}${COLOR_NONE}" | |
elif [ "$hg_branch" != "" ]; then | |
BRANCH=" ${LIGHT_GREEN}${hg_branch}${COLOR_NONE}" | |
else | |
BRANCH="" | |
fi | |
} | |
# Return the prompt symbol to use, colorized based on the return value of the | |
# previous command. | |
function set_prompt_symbol () { | |
if test $1 -eq 0 ; then | |
PROMPT_SYMBOL=" \$" | |
else | |
PROMPT_SYMBOL=" ${LIGHT_RED}\$${COLOR_NONE}" | |
fi | |
} | |
# Determine active Python virtualenv details. | |
function set_virtualenv () { | |
if test -z "$VIRTUAL_ENV" ; then | |
PYTHON_VIRTUALENV="" | |
else | |
PYTHON_VIRTUALENV=" ${BLUE}`basename \"$VIRTUAL_ENV\"`${COLOR_NONE}" | |
fi | |
} | |
# Determine active Anaconda env details | |
function set_anacondaenv () { | |
if test -z "$CONDA_PREFIX" ; then | |
CONDA_VIRTUALENV="" | |
else | |
conda_prefix="$CONDA_PREFIX" | |
CONDA_VIRTUALENV=" conda>${BLUE}${conda_prefix##*/}${COLOR_NONE}" | |
fi | |
} | |
# Set the full bash prompt. | |
function set_bash_prompt () { | |
# Set the PROMPT_SYMBOL variable. We do this first so we don't lose the | |
# return value of the last command. | |
set_prompt_symbol $? | |
# Set the PYTHON_VIRTUALENV and CONDA_VIRTUALENV variable. | |
set_virtualenv | |
set_anacondaenv | |
# Set the BRANCH variable. | |
set_git_branch | |
# Set the bash prompt variable. | |
PS1="[${USERNAME}\u${COLOR_NONE}@\h${COLOR_NONE} ${DIRCOLOR}\W${COLOR_NONE}${PYTHON_VIRTUALENV}${CONDA_VIRTUALENV}${BRANCH}]${PROMPT_SYMBOL} " | |
} | |
# Tell bash to execute this function just before displaying its prompt. | |
PROMPT_COMMAND=set_bash_prompt | |
force_color_prompt=yes | |
# Set Dircolors/LS_COLORS | |
[ -e ~/.dircolors ] && eval $(dircolors -b ~/.dircolors) || eval $(dircolors -b) | |
# Custom aliases | |
alias cls='clear' | |
alias py='python' | |
export PATH="$PATH:~/sdk/go1.18.3/bin" | |
export GOPATH="/mnt/Data/goland_repo" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment