A better Bash command-prompt

Bash sets the command prompt using the variable PS1, which is evaluated every time the prompt is printed. The following script, stolen from various parts of the internet (sorry, I didn’t keep references), is quite handy. It lists the username, host, working directory, and git/svn branch.

#!/usr/bin/bash
if [[ $- == *i* ]] ; then
    function parse_git_branch () {
        git name-rev HEAD 2>/dev/null | sed 's#HEAD\ \(.*\)#git:\1#'
    }
    function parse_svn_branch() {
        local rurl=`svn info --show-item relative-url 2>/dev/null`
        [[ -n "$rurl" ]] && echo "svn:${rurl}@`svn info --show-item revision`"
    }

    function proml() {
        local BOLD="\[$(tput bold)\]"
        local RED="\[$(tput setaf 1)\]"
        local GREEN="\[$(tput setaf 2)\]"
        local YELLOW="\[$(tput setaf 3)\]"
        local BLUE="\[$(tput setaf 4)\]"
        local PURPLE="\[$(tput setaf 5)\]"
        local CYAN="\[$(tput setaf 6)\]"
        local WHITE="\[$(tput setaf 7)\]"
        local GRAY="\[$(tput setaf 8)\]"
        local DEFAULT="\[$(tput sgr0)\]"

        PS1="${PURPLE}\u${GREEN}@\h ${YELLOW}\w${CYAN} "
        PS1+="\`parse_git_branch\`\`parse_svn_branch\`${DEFAULT}\n> "
    }
    proml
fi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s