Skip to content

Instantly share code, notes, and snippets.

@rgs
Created November 8, 2013 12:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rgs/7370406 to your computer and use it in GitHub Desktop.
Save rgs/7370406 to your computer and use it in GitHub Desktop.
A vim status line item to display git branch name and status of the currently edited file. To be put in the ~/.vimrc.
" returns a string <branch/XX> where XX corresponds to the git status
" (for example "<master/ M>")
function CurrentGitStatus()
let gitoutput = split(system('git status --porcelain -b '.shellescape(expand('%')).' 2>/dev/null'),'\n')
if len(gitoutput) > 0
let b:gitstatus = strpart(get(gitoutput,0,''),3) . '/' . strpart(get(gitoutput,1,' '),0,2)
else
let b:gitstatus = ''
endif
endfunc
autocmd BufEnter,BufWritePost * call CurrentGitStatus()
" example of use in the status line:
set stl=%f\ %(<%{b:gitstatus}>%)
@bla-rs
Copy link

bla-rs commented Mar 27, 2020

Hey there,
i found your code snippet pretty useful but also, that i wont work properly for vim working directories outside of a git repository.

Example:
vim outside/git_repo/some.file
... won't display the branch even thou it's a repository.

I made two small commits to fix this.

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