Created
January 17, 2015 14:43
-
-
Save ahammar/38a9344d1a6727e5a479 to your computer and use it in GitHub Desktop.
Custom Bash prompt showing current Git branch and exit code for failed commands
This file contains hidden or 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
# put this in your .bashrc | |
PROMPT_COMMAND=__prompt_command | |
__prompt_command() { | |
local LAST_EXIT="$?" | |
PS1="" | |
local RED='\[\e[31m\]' | |
local GREEN='\[\e[01;32m\]' | |
local BLUE='\[\e[01;34m\]' | |
local PURPLE='\[\e[01;35m\]' | |
local NORMAL='\[\e[0m\]' | |
# Set title of terminal window | |
case "$TERM" in | |
xterm*|rxvt*) | |
PS1+="\[\e]0;\w\a\]" | |
;; | |
*) | |
;; | |
esac | |
# Show exit status of last command if unsuccessful | |
if [ $LAST_EXIT != 0 ]; then | |
PS1+="${RED}✗ ${LAST_EXIT}\n" | |
fi | |
# Show host and working directory | |
PS1+="${GREEN}\h${NORMAL}:${BLUE}\w" | |
# Show current git branch if applicable | |
local CURRENT_BRANCH="$(git branch 2> /dev/null | sed -n 's/* //p')" | |
if [ "$CURRENT_BRANCH" ]; then | |
PS1+=" ${PURPLE}[${CURRENT_BRANCH}]" | |
fi | |
# Finally show dollar prompt on a separate line | |
PS1+="${NORMAL}\n\$ " | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment