Skip to content

Instantly share code, notes, and snippets.

@DavidEGx
Last active January 11, 2019 11:56
Show Gist options
  • Save DavidEGx/9226333 to your computer and use it in GitHub Desktop.
Save DavidEGx/9226333 to your computer and use it in GitHub Desktop.
Bash prompt with git, error and background jobs information
# Description
# ===========
# This will change your prompt to something useful like:
#
# david@dgarciapc/fix-flux-capacitor:~/projects/manhattan #2 :) $
# ----- ----- ----- ----- - |
# | | | | |
# user hostname git branch current folder jobs last command result
#
# If the last command exit with an error the last bit will change to :(
#
# Prompt examples
# ===============
# david@dgarciapc:~ #0 :) $ pwd
# /home/david
# david@dgarciapc:~ #0 :) $ cd ~/projects/manhattan
# david@dgarciapc/fix-flux-capacitor:~/projects/manhattan #0 :) $ git checkout master
# david@dgarciapc/master:~/projects/manhattan #0 :) $ yes > /dev/null &
# david@dgarciapc/master:~/projects/manhattan #1 :) $ yes > /dev/null &
# david@dgarciapc/master:~/projects/manhattan #2 :) $ jobs -l
# [1]- 11913 Running yes > /dev/null &
# [2]+ 14090 Running yes > /dev/null &
# david@dgarciapc/master:~/projects/manhattan #2 :) $ kill 14090
# [2]+ Terminated yes > /dev/null
# david@dgarciapc/master:~/projects/manhattan #1 :) $ sl
# bash: sl: command not found
# david@dgarciapc/master:~/projects/manhattan #1 :( $ ls
# BUILD UTIL PROJECT_X
# david@dgarciapc/master:~/projects/manhattan #1 :) $
#
# Usage
# =====
# 1. Edit your ~/.bashrc file
# 2. Append the code
# 3. Source the ~/.bashrc file ($ source ~/.bashrc) or open a new terminal
#
############################
### Prompt configuration ###
############################
# Need to get git-prompt:
# wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -O ~/.git-prompt.sh
if [ -f ~/.git-prompt.sh ]; then
. ~/.git-prompt.sh
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWCOLORHINTS=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWUPSTREAM="auto"
fi
# Colors
PR_PLAIN='\[\e[0m\]'
PR_RED='\[\e[31m\]'
PR_GREEN='\[\e[32m\]\'
# Basic stuff
PR_BASE='\[\033[01;32m\]\u@\h\[\033[00m\]'
PR_GIT='$(__git_ps1 "/%s")'
PR_DIR='\[\033[01;34m\]\w\[\033[00m\]'
# To show the number of jobs
PR_JOBS='
case "\j" in
0) echo "";;
1) echo "𝍠 " ;;
2) echo "𝍡 " ;;
3) echo "𝍢 " ;;
4) echo "𝍣 " ;;
*) echo "𝍤 " ;;
esac
'
# To show ok or ko depeding of result of previous command
PR_CMD_RESULT="
if [ \$LASTCMD -eq 0 ]; then
echo ${PR_GREEN}✔${PR_PLAIN};
else
echo ${PR_RED}❌${PR_PLAIN};
fi
"
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]$(__git_ps1 "/%s"):\[\033[01;34m\]\w\[\033[00m\]\$ '
PROMPT_COMMAND='LASTCMD=$? PS1="${debian_chroot:+($debian_chroot)}${PR_BASE}${PR_GIT}:${PR_DIR} \`${PR_JOBS}\` \`${PR_CMD_RESULT}\` \\$ "'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment