Skip to content

Instantly share code, notes, and snippets.

@saccarosium
Last active April 16, 2025 12:28
Show Gist options
  • Save saccarosium/20369d5e5d5bddfb4ec0f62fac7f80dc to your computer and use it in GitHub Desktop.
Save saccarosium/20369d5e5d5bddfb4ec0f62fac7f80dc to your computer and use it in GitHub Desktop.
Fancy bash prompt
has() { type "$1" >/dev/null 2>&1; }
__ps1_git() {
local B S C c u z
# If not in working tree return
git rev-parse --is-inside-work-tree &>/dev/null || return
B=$(git branch --show-current 2>/dev/null)
# If there is no branch your are detached
[[ -z $B ]] && B=$(git rev-parse --short HEAD)
S=$(git status --porcelain -s 2>/dev/null)
# Local changes
[[ $S == ' A'* || $S == ' M'* || $S == ' D'* ]] && c='*'
# Untracked files
[[ $S == *'??'* ]] && u='%'
# Stashed files
git rev-parse --verify -q refs/stash &>/dev/null && z='$'
C="$c$u$z"
[[ -n $C ]] && C=" $C"
printf '(%s%s)' "$B" "$C"
}
__ps1() {
history -a
PROMPT_DIRTRIM=2
# Sync pwd in vte based terminals
declare -F __vte_osc7 &>/dev/null && __vte_osc7 2>/dev/null
local r='\[\e[31m\]' gr='\[\e[0;32m\]' y='\[\e[33m\]' \
bl='\[\e[34m\]' pu='\[\e[0;35m\]' x='\[\e[0m\]' \
G E N P C
has nix && [[ -n $IN_NIX_SHELL || "$PATH" == *'/nix/store'* ]] && N='(nix)'
has git && G="$(__ps1_git)"
[ -n "$CONTAINER_ID" ] && C="[$CONTAINER_ID]"
[ -n "$VIRTUAL_ENV" ] && E="(${VIRTUAL_ENV##*/})"
P="$gr\u@\h$C$x:$bl\w$pu$N$y$E$r$G$x"
[ $COLUMNS -gt 80 ] && PS1="$P\$ " || PS1="$P\n\$ "
}
PROMPT_COMMAND="__ps1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment