I use CTRL-W c to close current window. I expected that 5 CTRL-W c close 5 windows in row, but it's not. Now I just type CTRL-W c 5 times.

What is correct way to do it and why it don't work as I expected?

  • 2
    In case you want to close all window, except the current window, then do :only or <c-w>o. See :h :only – Peter Rincker Feb 15 '19 at 15:09
  • Also, ZZ and any of the other quit/exit mechanisms close a window (unless it’s the last window, and then they quit) – D. Ben Knoble Feb 15 '19 at 15:15

First, giving a count to the command doesn't work as you expected because the doc (:h CTRL-W_c) says:

Without {count}: Close the current window.  If {count} is
given close the {count} window.

Now if you want to create a mapping calling <C-w>c N times you could add this to your vimrc:

nnoremap <F1> :<C-u>for i in range(v:count1) \| close \| endfor<CR>

Replace <F1> by the key you want to use in your mapping. What is line does is the following:

:                           Go to command line mode
<C-u>                       Remove the range automatically added to the command line
for i in range(v:count1)    Create a loop from 0 to the count you gave to the mapping (or 1 if you didn't gave a count)
close                       Close the current window
endfor                      End of the loop
<CR>                        Validate the command

Now you can press <F1> to close the current window and 4<F1> to close the current window and the 3 next current windows.

You can read the following related help topics:

| improve this answer | |
  • The correct help item should be :h c_CTRL-U instead... :h CTRL-U is about page up in normal mode... – filbranden Feb 15 '19 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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