If you want to perform asynchronous tasks from Vim, people generally recommend using the vim-dispatch
plugin to run external processes in the background and call back later.
But since I don't have an example for that, I'm going to offer some dirty synchronous tricks instead...
You could consider only doing the work when the user has gone idle, by waiting for the CursorHold
event.
let g:git_status = "..."
let &statusline = "blah blah %{g:git_status} blah blah"
augroup Get_Git_Status
autocmd!
autocmd CursorHold * let g:git_status = fugitive#statusline()
augroup END
Add an identical CursorHoldI
line, if you also want the updates to happen when you are idle in Insert mode.
Alternatively, but slightly longer, I call a function to cache the value and only update it once every 10 seconds. You could adapt that to call fugitive#statusline()
. And you could, by storing a timestamp at startup, only allow polling after Vim has been open for a few seconds.
I do not recall right now when and why statusline
gets evaluated, or how frequently.
A combination of CursorHold
and caching would probably have the least noticeable impact on Vim's performance. Although just fetching the git branch is not that heavy a task anyway, especially after the first time.
For heavier work, for example checking the status of the entire working tree, I really would recommend a separate "dispatched" process.
git branch|grep...
asynchronously, and when the result comes back inject it into the statusline before refreshing it.