-
-
Save donovanh/5814722 to your computer and use it in GitHub Desktop.
A handy bash prompt customization that shows the working directory followed by the current branch, and an asterisk if the branch is dirrrrty.
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
# Highlighting on prompt | |
RED="\[\033[0;31m\]" | |
YELLOW="\[\033[0;33m\]" | |
GREEN="\[\033[0;32m\]" | |
COLOR_NONE="\[\e[0m\]" | |
parse_git_branch() { | |
git_status="$(git status 2> /dev/null)" | |
branch_pattern="^# On branch ([^${IFS}]*)" | |
remote_pattern="# Your branch is (.*) of" | |
diverge_pattern="# Your branch and (.*) have diverged" | |
if [[ ! ${git_status}} =~ "working directory clean" ]]; then | |
state="${RED} " | |
fi | |
# add an else if or two here if you want to get more specific | |
if [[ ${git_status} =~ ${remote_pattern} ]]; then | |
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then | |
remote="${YELLOW}↑" | |
else | |
remote="${YELLOW}↓" | |
fi | |
fi | |
if [[ ${git_status} =~ ${diverge_pattern} ]]; then | |
remote="${YELLOW}↕" | |
fi | |
if [[ ${git_status} =~ ${branch_pattern} ]]; then | |
branch=${BASH_REMATCH[1]} | |
echo " (${branch})${remote}${state}" | |
fi | |
} | |
prompt_func() { | |
git rev-parse --git-dir &> /dev/null | |
previous_return_value=$? | |
prompt="\w${GREEN}$(parse_git_branch)${COLOR_NONE} " | |
if [[ $previous_return_value == 0 ]]; then | |
PS1="${prompt}\$ " | |
else | |
PS1=$PS1_DEFAULT | |
fi | |
} | |
PS1_DEFAULT=$PS1 | |
PROMPT_COMMAND=prompt_func | |
set_tab_title() { | |
echo -n -e "\033]0;${PWD##*/}\007" | |
} | |
PROMPT_COMMAND="set_tab_title ; $PROMPT_COMMAND" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok. Forget this - get on board with zsh and oh-my-zsh instead :D
https://github.com/robbyrussell/oh-my-zsh