Skip to content

Instantly share code, notes, and snippets.

@bla-rs
Forked from rgs/gist:7370406
Last active December 19, 2020 01:43
Show Gist options
  • Save bla-rs/c439daa0aaa5dea899056bc0b7d34ead to your computer and use it in GitHub Desktop.
Save bla-rs/c439daa0aaa5dea899056bc0b7d34ead 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 = systemlist('cd '.expand('%:p:h:S').' && git status --porcelain -b 2>/dev/null')
if len(gitoutput) > 0
let b:gitstatus = strpart(get(gitoutput,0,''),3) .'/'. strpart(get(gitoutput,1,' '),1,2)
else
let b:gitstatus = ''
endif
endfunc
autocmd BufEnter,BufWritePost * call CurrentGitStatus()
" example of use in the status line:
set stl=%f\ %(<%{b:gitstatus}>%)
Copy link

ghost commented Aug 3, 2020

I was messing around trying to awk the output of :Git status from fugitive, and I found your code. really helped me! thanks.

function! MyFugitive()
    let _ = fugitive#head()
    if exists("g:gitstatus")
        if g:gitstatus == "true"
            return strlen(_) ? "   "._ : ""
        else
            return strlen(_) ? "   "._ : ""
        endif
    else
        return
    endif
endfunction

function! GetGitStatus()
    let gitoutput = systemlist('cd '.expand('%:p:h:S').' && git status --porcelain -b 2>/dev/null | grep M')
    if len(gitoutput) > 0
        let g:gitstatus = "true"
    else
        let g:gitstatus = "false"
    endif
endfunc

autocmd BufEnter,BufWritePost * call GetGitStatus()

I don't tested so much but seems like this works for lightline. idk but it's working now.

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