Skip to content

Instantly share code, notes, and snippets.

@rkitover
Last active February 22, 2021 23:28
Show Gist options
  • Save rkitover/3efb2312fa97987705cd466ebb78432d to your computer and use it in GitHub Desktop.
Save rkitover/3efb2312fa97987705cd466ebb78432d to your computer and use it in GitHub Desktop.
simple version of bash git prompt for ksh (also works in bash) (raw escape, click raw and fetch the gist URL with curl or wget)
shopt -s checkwinsize 2>/dev/null
_cmd_status() {
if [ $? -eq 0 ]; then
printf '\001\002✔'
else
printf '\001\002✘'
fi
}
_git_branch() {
_br="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
if [ -n "$_br" ]; then
printf '\001\002[\001\002'"${_br}"'\001\002]\001\002'
fi
}
PS1='`_cmd_status` \001\002\w `_git_branch`\n\001\002'"${USER}"'\001\002@\001\002\h \001\002➤\001\002 '
@Mno-hime
Copy link

With older version of this script I sometimes got -bash: [: 1: unary operator expected after various git commands:

++ history -a
++ set_prompt
++ git --no-pager branch --no-color
+++ _git_branch
+++ local i c=1 only_local_ref=n has_r=n
+++ '[' 1 -lt ']'
-bash: [: 1: unary operator expected

Perhaps there's namespace clash with bash completion project:

/usr/share/bash-completion/completions/git:_git_branch ()
/usr/share/bash-completion/completions/gitk:_git_branch ()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment