I want to close a window by mentioning window name in command line.

For example, let's assume I have opened 1.c, 2.c and 3.c in different windows in vim. Now i'm in 2.c and i want to close 3.c. Is there a way i can close it like e.g. :close 3.c?

  • 1
    what happens, if you have the same file opened in several windows? Apr 24 '19 at 10:44

You need to use :bdelete 3.c or in short form :bd 3.c

There are a couple of terms you need to understand:

A buffer is the in-memory text of a file.
A window is a viewport on a buffer.
A tab page is a collection of windows.

So, what you're trying to do is close a buffer, not a window. The available commands to manipulate buffers are

  • :buffers or :ls or :files : See the list of opened buffers
  • :badd : Add a file to the buffer list
  • :bdelete : Remove buffer from the buffer list
  • :bwipeout : Like :bdelete, but really delete the buffer. And many more. You can find all about them from :help windows.txt
  • It's possible that the OP does know the difference between a buffer and a window, and really does want to close the window and not delete the buffer. I'm guessing that's not what they mean though, so +1.
    – Rich
    May 24 '19 at 14:17
  • If they do mean that, this is one way to do it: :windo if bufname('%') == '3.c' | q | endif
    – Rich
    May 24 '19 at 14:25

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.