Skip to content

Instantly share code, notes, and snippets.

@trustin
Last active July 27, 2018 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trustin/fbb8788e0d7107c566a5d34d0c37d3d1 to your computer and use it in GitHub Desktop.
Save trustin/fbb8788e0d7107c566a5d34d0c37d3d1 to your computer and use it in GitHub Desktop.
Trustin's Bash prompt
#!/bin/bash
# Git-aware bash command prompt
#
# Put this script at $HOME/.bash_prompt and add the following to your .bashrc:
#
# if [[ -f "$HOME/.bash_prompt" ]]; then
# if [[ -z "$PROMPT_COMMAND" ]]; then
# PROMPT_COMMAND="$HOME/.bash_prompt"
# else
# PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND ;} $HOME/.bash_prompt"
# fi
# if [[ "$EUID" == "0" ]]; then
# PS1="\n# "
# else
# PS1="\n$ "
# fi
# fi
# Terminal title
printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"
# Current user, host name, and working directory
printf "%s@%s:%s" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"
# Git information
GIT_STATUS="$(git status 2> /dev/null)"
if [[ "$?" == "0" ]]; then
echo
# Origin
GIT_ORIGIN="$(git config remote.origin.url)"
if [[ ${GIT_ORIGIN} =~ (^[^:]+://[^/]+/([-_a-zA-Z0-9/\.]*)\.git$) ]]; then
GIT_ORIGIN="${BASH_REMATCH[2]}"
elif [[ ${GIT_ORIGIN} =~ (^.*:/?([-_a-zA-Z0-9/\.]*)\.git$) ]]; then
GIT_ORIGIN="${BASH_REMATCH[2]}"
fi
if [[ -n "$GIT_ORIGIN" ]]; then
echo -ne "[git:\033[1;34m${GIT_ORIGIN}\033[0m]"
else
echo -n "[git]"
fi
# Highlight with a color
echo -ne "\033[1;32m"
# Various status
if [[ ${GIT_STATUS} =~ (^(# *)?On branch ([-_a-zA-Z0-9/\.]*)) ]]; then
echo -n " ${BASH_REMATCH[3]}"
fi
if [[ ${GIT_STATUS} =~ (working (tree|directory) clean) ]]; then
echo -n " clean"
else
echo -n " dirty"
fi
if [[ ${GIT_STATUS} =~ (Your branch is ([a-z]*) of ([-_\'a-zA-Z0-9/\.]*) by ([0-9]*) commit) ]]; then
echo -n " ${BASH_REMATCH[2]}:${BASH_REMATCH[4]}"
fi
if [[ ${GIT_STATUS} =~ (Your branch and ([^ ]*) have diverged) ]]; then
echo -n " diverged"
fi
echo -ne "\033[0m"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment