3

I can directly use the following approach to get the git branch information:

...
let l:git = "%6*%{exists('g:loaded_fugitive')?fugitive#statusline():''}%*"
...

If so, it can cause a delay to the startup of vim, I guess tens to a hundred milliseconds. With the async feature in Vim8, can I get this asynchronously? I am not conversant with this feature, could someone give me some tips? A mini example would be better, thanks!

1
  • 1
    The only things that can be run asynchronously are external processes. We cannot run VimL functions nor vim commands asynchronously. I'm afraid you'll have to run git branch|grep... asynchronously, and when the result comes back inject it into the statusline before refreshing it. Commented Jan 12, 2017 at 10:16

1 Answer 1

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.