0

Is it possible to auto close the Gstatus window after Gcommit?

I found some code, but I can't find out how to make it work.

function! s:close_gstatus()
for l:winnr in range(1, winnr('$'))
    if !empty(getwinvar(l:winnr, 'fugitive_status'))
        execute l:winnr.'close'
    endif
endfor
endfunction
qwatros
  • 13
  • 2
  • What do you mean by "auto close"? Do you not want it to open at all or do you want it to close after an event (say, after a loss of focus)? – haridsv Sep 11 '19 at 10:47
  • Yes, after a commit event. Usually I do 'cc' in Gstatus window. It opens a new commit window, but Gstatus left open also. – qwatros Sep 11 '19 at 13:37

1 Answers1

1

To invoke your code automatically based on some events, there is a mechanism called autocommands. With that, you can invoke functions, commands or expressions when some specific events fired.

The example below is to invoke the function when a COMMIT_EDITMSG buffer is deleted.

" .vimrc
augroup my_fugitive_commit_hook
  autocmd!
  autocmd BufDelete COMMIT_EDITMSG call s:close_gstatus()
augroup END

All event list can be found with :h autocommand-events.

Some more considerations

Not that the autocmd definition above closes the status window when the COMMIT_EDITMSG buffer is closed at any time even if the commit failed due to empty commit message for example. To improve this behaviour you need to check if git commit is actually submitted and succeeded somehow. My 2 cents for this is that is checking the COMMIT_EDITMSG buffer contained appropriate commit message lines and was actually saved using BufWritePost autocommand. E.g.

autocmd BufWritePost COMMIT_EDITMSG call s:set_commit_buffer_status()
Tacahiroy
  • 643
  • 4
  • 16