Skip to content

Instantly share code, notes, and snippets.

@wolever
Created September 11, 2013 15:41
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save wolever/6525437 to your computer and use it in GitHub Desktop.
Save wolever/6525437 to your computer and use it in GitHub Desktop.
My very fast Bash prompt, which shows git branch, virtualenv, and background jobs
# prompt examples:
# [3 jobs master virtualenv] ~/code/myproject/foo
# [1 job my-branch virtualenv] ~/code/bar/
# [virtualenv] ~/code/
# ~
# Very, very fast, only requiring a couple of fork()s (and no forking at all to determine the current git branch)
if [[ "$USER" == "root" ]]
then
export PS1="\e[1;31m\]\u \[\e[1;33m\]\w\[\e[0m\] ";
else
export PS1="\[\e[1;33m\]\w\[\e[0m\] ";
fi
# 100% pure Bash (no forking) function to determine the name of the current git branch
gitbranch() {
export GITBRANCH=""
local repo="${_GITBRANCH_LAST_REPO-}"
local gitdir=""
[[ ! -z "$repo" ]] && gitdir="$repo/.git"
# If we don't have a last seen git repo, or we are in a different directory
if [[ -z "$repo" || "$PWD" != "$repo"* || ! -e "$gitdir" ]]; then
local cur="$PWD"
while [[ ! -z "$cur" ]]; do
if [[ -e "$cur/.git" ]]; then
repo="$cur"
gitdir="$cur/.git"
break
fi
cur="${cur%/*}"
done
fi
if [[ -z "$gitdir" ]]; then
unset _GITBRANCH_LAST_REPO
return 0
fi
export _GITBRANCH_LAST_REPO="${repo}"
local head=""
local branch=""
read head < "$gitdir/HEAD"
case "$head" in
ref:*)
branch="${head##*/}"
;;
"")
branch=""
;;
*)
branch="d:${head:0:7}"
;;
esac
if [[ -z "$branch" ]]; then
return 0
fi
export GITBRANCH="$branch"
}
PS1_green='\[\e[32m\]'
PS1_blue='\[\e[34m\]'
PS1_reset='\[\e[0m\]'
_mk_prompt() {
# Change the window title of X terminals
case $TERM in
xterm*|rxvt*|Eterm)
echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\007"
;;
screen)
echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\033\\"
;;
esac
# Un-screw virtualenv stuff
if [[ ! -z "${_OLD_VIRTUAL_PS1-}" ]]; then
export PS1="$_OLD_VIRTUAL_PS1"
unset _OLD_VIRTUAL_PS1
fi
if [[ -z "${_MK_PROMPT_ORIG_PS1-}" ]]; then
export _MK_PROMPT_ORIG_PS1="$PS1"
fi
local prefix=()
local jobcount="$(jobs -p | wc -l)"
if [[ "$jobcount" -gt 0 ]]; then
local job="${jobcount##* } job"
[[ "$jobcount" -gt 1 ]] && job="${job}s"
prefix+=("$job")
fi
gitbranch
if [[ ! -z "$GITBRANCH" ]]; then
prefix+=("${PS1_green}$GITBRANCH${PS1_reset}")
fi
local virtualenv="${VIRTUAL_ENV##*/}"
if [[ ! -z "$virtualenv" ]]; then
prefix+=("${PS1_blue}$virtualenv${PS1_reset}")
fi
PS1="$_MK_PROMPT_ORIG_PS1"
if [[ ! -z "$prefix" ]]; then
PS1="[${prefix[@]}] $PS1"
fi
export PS1
}
export PROMPT_COMMAND=_mk_prompt
@wamatt
Copy link

wamatt commented Feb 18, 2015

Definitely quite fast. only problem is it stills says "master", even if I go outside a git repo, eg into /tmp

@jaknel
Copy link

jaknel commented Jun 18, 2015

http://lists.gnu.org/archive/html/bug-bash/2014-03/msg00036.html discusses a fix for an old bash bug regarding tilde ~ expansion introduced between bash 4.2 and bash 4.3 which changes the behavior of this code on line 68 and line 71.

To share my .bashrc between two machines running different versions of bash I had to add a variable just to use for tilde replacement. Otherwise escape chars or quoting work on one version, but print or expand improperly on another.

    case $TERM in
        xterm*|rxvt*|Eterm)
        local tilde="~"
        if [ $USER == "root" ];
        then
            echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/$tilde}\007"
        else

            echo -ne "\033]0;${HOSTNAME%%.*}:${PWD/$HOME/$tilde}\007"
        fi
        ;;
    screen)
        echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/$HOME/$tilde}\033\\"
        ;;
    esac

@dubglan
Copy link

dubglan commented Jun 27, 2015

Also to work correctly under tmux default PS1 for root (line 10) should be
export PS1="\[\e[1;31m\]\u\[\e[0m\] \[\e[1;33m\]\w\[\e[0m\] ";
note \[ in the beginning

@codemedic
Copy link

Thanks for posting this; very useful.

L52 .. What does that do?
Just trying to understand your script.

@Ragnoroct
Copy link

Thank you for this gist. It really made a difference on windows using msys2 (~80ms down to ~3ms)
I modified it to be simpler and it can be found here https://gist.github.com/Ragnoroct/c4c3bf37913afb9469d8fc8cffea5b2f

@patricknelson
Copy link

@Ragnoroct's gist ☝️ is indeed super fast; it also omits the branch if you're not in a git repository. I modified that even further to optimize speed slightly more with PROMPT_COMMAND. Thanks so much for the effort on this by the folks above (for me it would have been impractical, if not impossible, otherwise)!

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