Command prompt functions to go in .bashrc or .bash_profile that will color the command line if there is a git repository.
This file contains 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
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# THIS IS NOW LOCATED AT https://raw.githubusercontent.com/zkamvar/config-files/master/.bash_prompt | |
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# color prompt to include branch information | |
function color_my_prompt { | |
local __user_and_host="\[\033[01;32m\]\u@\h" | |
local __cur_location="\t\w" | |
local __git_branch='`git symbolic-ref HEAD --short | sed -E s/^\(.+\)$/\(\\\\\1\)\/`' | |
local __prompt_tail="$" | |
local __last_color="\[\033[00m\]" | |
RED="\[\033[0;31m\]" | |
YELLOW="\[\033[0;33m\]" | |
LPURPLE="\[\033[0;35m\]" | |
GREEN="\[\033[0;32m\]" | |
LBLUE="\[\033[0;34m\]" | |
# Capture the output of the "git status" command. | |
git_status="$(git status 2> /dev/null)" | |
# Set color based on clean/staged/dirty. | |
if [[ ${git_status} =~ "othing to commit" ]]; then | |
#state="${GREEN}" | |
state="${LBLUE}" | |
elif [[ ${git_status} =~ "Changes to be committed" ]]; then | |
#state="${YELLOW}" | |
state="${LPURPLE}" | |
else | |
state="${RED}" | |
fi | |
# collect the number of commits ahead or behind origin | |
sym="$(echo \"${git_status}\" | sed -E 's/.*by[[:blank:]]([0-9]*)[[:blank:]]commit.*/\1/' | grep '[0-9]' 2> /dev/null)" | |
# add marker for origin status with number of commits ahead (+) or behind (-) | |
if [[ ${git_status} =~ "branch is ahead of" ]]; then | |
sym="[+"${sym}"]" | |
elif [[ ${git_status} =~ "branch is behind" ]]; then | |
sym="[-"${sym}"]" | |
else | |
sym="" | |
fi | |
export PS1="$__cur_location\n${state}$__git_branch${sym}$__last_color$__prompt_tail " | |
} | |
# Tell bash to execute this function just before displaying its prompt. | |
PROMPT_COMMAND=color_my_prompt |
Link to the current version: https://raw.githubusercontent.com/zkamvar/config-files/master/.bash_prompt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist was modified from: https://github.com/Reproducible-Science-Curriculum/Reproducible-Science-Hackathon-Dec-08-2014/wiki/Trick-to-color-bash-prompt-with-git-branch