Vim: Show current git branch in lightline status line without fugitive

Robert Audi picture Robert Audi · Jun 4, 2014 · Viewed 16.1k times · Source

I am using lightline.vim and I would like to show the current git branch. At the moment, I am doing so using vim-fugitive, however, I do not use this plugin at all, except for showing the git branch in the status line.

My question is:

Is there a way to show the git branch in the status line without using fugitive?

I tried to create a custom function that I hooked to lightline. The function fetched the current git branch using the system vimscript function. It (didn't) work well, except that the status line was refreshed on every keystroke, which broke everything else and made vim unusable. Essentially, the git branch was being fetched using the system function on every keystroke...

So that's not a viable solution. What I "could" do, is create a global variable which says whether or not the git branch was fetched, and prevent the custom function from fetching the branch name again if it was already fetched. Then I could fetch the branch name again every time MacVim loses focus (not sure how to do that using terminal vim). That is not ideal though...

Any ideas on how to solve this problem please?

Answer

itchyny picture itchyny · Jun 10, 2014

I have created a plugin which focuses on the function you want.

https://github.com/itchyny/vim-gitbranch

You can get the branch name by gitbranch#name() function.

The code is so small that you can read in a minute.