Skip to content

Instantly share code, notes, and snippets.

@machalvan
Last active June 22, 2020 16:06
Show Gist options
  • Save machalvan/ba3f85556af84f3cf0c2472c69acbdd5 to your computer and use it in GitHub Desktop.
Save machalvan/ba3f85556af84f3cf0c2472c69acbdd5 to your computer and use it in GitHub Desktop.
A fancy bash prompt in colors
# A fancy bash prompt in colors
#
# Shows user, host, current directory and git branch (if any).
# Ex. halvan@arch > ~/Code/my-project > master >
#
# The  character is a right pointing arrow simular to ▶,
# replace it with your character of choice if it does not
# display as an arrow.
# Colors
GREEN="\[$(tput setaf 2)\]"
YELLOW="\[$(tput setaf 3)\]"
DARK_GRAY="\[$(tput setaf 234)\]"
GRAY="\[$(tput setaf 236)\]"
LIGHT_GRAY="\[$(tput setaf 238)\]"
BG_DARK_GRAY="\[$(tput setab 234)\]"
BG_GRAY="\[$(tput setab 236)\]"
BG_LIGHT_GRAY="\[$(tput setab 238)\]"
RESET="\[$(tput sgr0)\]"
# Get current checked out git branch
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/';
}
# Show current git branch in colors (if any)
fancy_git_branch() {
if [ "$(parse_git_branch)" != "" ]; then
echo "${BG_LIGHT_GRAY}${GRAY} ${BG_LIGHT_GRAY}${YELLOW}$(parse_git_branch) ${RESET}${LIGHT_GRAY}";
else
echo "${RESET}${GRAY}";
fi
}
# The prompt consists of three parts:
# 1. user@host - Dark gray background, white text
# 2. Current path - Gray background, green text
# (3. Git branch - Light gray background, yellow text)
export PS1="$BG_DARK_GRAY\u@\h $BG_GRAY$DARK_GRAY $GREEN\w $(fancy_git_branch)$RESET "
@machalvan
Copy link
Author

Example:
image

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