Skip to content

Instantly share code, notes, and snippets.

@bgr
Last active December 31, 2015 13:58
Show Gist options
  • Save bgr/7996141 to your computer and use it in GitHub Desktop.
Save bgr/7996141 to your computer and use it in GitHub Desktop.
My Bash prompt format
#!/bin/bash
# source this in .bashrc
# changes PS1 prompt format to:
#
# ┌(user@host)─(err_code)─(time date)─(git branch)─(virtualenv)
# └─(~/working/directory) $
#
# it's based on:
# http://bash.cyberciti.biz/guide/Changing_bash_prompt
# https://wiki.archlinux.org/index.php/Color_Bash_Prompt
GIT_PS1_SHOWDIRTYSTATE='yes'
GIT_PS1_SHOWSTASHSTATE='yes'
source ~/git-prompt.sh
# git-prompt.sh might be present on the system, otherwise it can be downloaded
# from https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh
bgr_update_PS1() {
# colors
local NONE="\[\033[0m\]" # unsets color to term's fg color
local K="\[\033[0;30m\]" # black
local R="\[\033[0;31m\]" # red
local G="\[\033[0;32m\]" # green
local Y="\[\033[0;33m\]" # yellow
local B="\[\033[0;34m\]" # blue
local M="\[\033[0;35m\]" # magenta
local C="\[\033[0;36m\]" # cyan
local W="\[\033[0;37m\]" # white
# emphasized (bolded) colors
local EMK="\[\033[1;30m\]"
local EMR="\[\033[1;31m\]"
local EMG="\[\033[1;32m\]"
local EMY="\[\033[1;33m\]"
local EMB="\[\033[1;34m\]"
local EMM="\[\033[1;35m\]"
local EMC="\[\033[1;36m\]"
local EMW="\[\033[1;37m\]"
# background colors
local BGK="\[\033[40m\]"
local BGR="\[\033[41m\]"
local BGG="\[\033[42m\]"
local BGY="\[\033[43m\]"
local BGB="\[\033[44m\]"
local BGM="\[\033[45m\]"
local BGC="\[\033[46m\]"
local BGW="\[\033[47m\]"
local DASH="\342\224\200" # prettier version of - character
# shorten PWD
local pwdmaxlen=40
local trunc_symbol=".."
local dir=${PWD##*/}
pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
local short_pwd=${PWD/#$HOME/\~}
local pwdoffset=$(( ${#short_pwd} - pwdmaxlen ))
if [ ${pwdoffset} -gt "0" ]
then
short_pwd=${short_pwd:$pwdoffset:$pwdmaxlen}
short_pwd=${trunc_symbol}/${short_pwd#*/}
fi
# get git info if current dir is a git repo
local git=$(__git_ps1 '%s' )
#### assemble PS1 ####
# newline and ascii decoration
PS1="\n${EMK}\342\224\214"
# current user
local user=''
if [[ ${EUID} == 0 ]]; then
user="${EMR}\h"
else
user="${B}\u@\h"
fi
PS1=$PS1"${EMK}(${user}${EMK})"
# error code of last command
# has to be inlined since $? will be substituted later
PS1=$PS1"${EMK}${DASH}(\$(err=\$?; if [[ \$err != 0 ]]; then echo -n '$EMR'; else echo -n '$B'; fi; echo -n "\$err")${EMK})"
# time and date
PS1=$PS1"${EMK}${DASH}(${B}\t \D{%d/%m/%Y}${EMK})"
# git branch (if inside git repo)
if [ -n "$git" ]; then
PS1=$PS1"${EMK}${DASH}(${Y}${git}${EMK})"
fi
# python virtualenv (if currently active) (##*/ strips path)
if [ -n "$VIRTUAL_ENV" ]; then
PS1=$PS1"${EMK}${DASH}(${C}${VIRTUAL_ENV##*/}${EMK})"
fi
# newline and decoration
PS1=$PS1"\n${EMK}\342\224\224${DASH}"
# current directory
PS1=$PS1"(${EMG}${short_pwd}${EMK})"
# $ or #
local term=''
if [[ ${EUID} == 0 ]]; then
term="${EMR}#"
else
term="${B}$"
fi
PS1=$PS1" ${term}${NONE} "
# set terminal window title
echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"
}
PROMPT_COMMAND=bgr_update_PS1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment