vimvim-fugitive

Auto close Gstatus window after Gcommit or open Gcommit in the same window


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

Solution

  • 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()