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?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communitySince 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'
return
endif
endfor
keepalt Git
endfunction
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
).