Displaying Git's current branch in Bash and Vim

I messed up Git today by forking a wrong branch. To prevent issues like this I now display Git's current branch in both Bash and Vim.

Displaying current branch in Bash

My PS_1 config of ~/.bash_profile looks like this:

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
PS1="\\[$(tput setaf 7)\\]\\w \$(parse_git_branch)> \\[$(tput sgr0)\\]"

This displays the bash command line like this:

~/Desktop/PlurkGit/trunk (master)>

Displaying current branch in Vim

In Vim my status line looks like this:

Vim Status Line - Git branch

My statusline configuration looks like this:

"Git branch
function! GitBranch()
    let branch = system("git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* //'")
    if branch != ''
        return '   Git Branch: ' . substitute(branch, '\n', '', 'g')
    en
    return ''
endfunction

function! CurDir()
    return substitute(getcwd(), '/Users/amir/', "~/", "g")
endfunction

function! HasPaste()
    if &paste
        return 'PASTE MODE  '
    en
    return ''
endfunction

" Format the statusline
set statusline=\ %{HasPaste()}%F%m%r%h\ %w\ \ CWD:\ %r%{CurDir()}%h\ \ \ Line:\ %l/%L%{GitBranch()}

Code · Stuff · Tips · VIM Editor 6. Oct 2010
© Amir Salihefendic. Powered by Skeletonz.