Fugitive provides a :Git commands that opens a window with basically the output of git status. To close it, I need to switch to that window and close it, which is tedious.

How do I toggle this window?

1 Answer 1


Since Fugitive's :Git window always opens in the topleft position, we can simply do :1close (or even shorter :1clo) to close that window without switching to it.

But if we do want to a toggling functionality for it, we can utilize 'fugitive_status' window property/variable that Fugitive sets for its :Git window. If any of the opened windows have this variable, we close it; if none of the windows have it, we invoke :Git command.

function! ToggleGstatus() abort
  for l:winnr in range(1, winnr('$'))
    if !empty(getwinvar(l:winnr, 'fugitive_status'))
      exe l:winnr 'close'
  keepalt Git
nnoremap gs :call ToggleGstatus()<CR>

N.B. I used keepalt command so :Git doesn't affect the alternate buffer variable # (see :h alternate-file and :h :keepalt).

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.