Last active
June 22, 2020 16:06
-
-
Save machalvan/ba3f85556af84f3cf0c2472c69acbdd5 to your computer and use it in GitHub Desktop.
A fancy bash prompt in colors
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
# 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 " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example:
![image](https://user-images.githubusercontent.com/10708640/85308437-7f873580-b4b1-11ea-9cc7-542ae8670b5c.png)