Close file without quitting VIM application?

Vim

Vim Problem Overview


I use the :e and :w commands to edit and to write a file. I am not sure if there is "close" command to close the current file without leaving Vim?

I know that the :q command can be used to close a file, but if it is the last file, Vim is closed as well; Actually on Mac OS MacVim does quit. Only the Vim window is closed and I could use Control-N to open a blank Vim window again. I would like Vim to remain open with a blank screen.

Vim Solutions


Solution 1 - Vim

This deletes the buffer (which translates to close the file)

:bd 

Solution 2 - Vim

As already mentioned, you're looking for :bd, however this doesn't completely remove the buffer, it's still accessible:

:e foo
:e bar
:buffers
  1 #h   "foo"                          line 1
  2 %a   "bar"                          line 1
Press ENTER or type command to continue
:bd 2
:buffers
  1 %a   "foo"                          line 1
Press ENTER or type command to continue
:b 2
2   bar

You may instead want :bw which completely removes it.

:bw 2
:b 2 
E86: Buffer 2 does not exist

Not knowing about :bw bugged me for quite a while.

Solution 3 - Vim

If you have multiple split windows in your Vim window then :bd closes the split window of the current file, so I like to use something a little more advanced:

map fc <Esc>:call CleanClose(1)

map fq <Esc>:call CleanClose(0)


function! CleanClose(tosave)
if (a:tosave == 1)
    w!
endif
let todelbufNr = bufnr("%")
let newbufNr = bufnr("#")
if ((newbufNr != -1) && (newbufNr != todelbufNr) && buflisted(newbufNr))
    exe "b".newbufNr
else
    bnext
endif

if (bufnr("%") == todelbufNr)
    new
endif
exe "bd".todelbufNr
endfunction

Solution 4 - Vim

:[N]bd[elete][!]                        :bd :bdel :bdelete E516
:bd[elete][!] [N]
Unload buffer [N] (default: current buffer) and delete it from
the buffer list.  If the buffer was changed, this fails,
unless when [!] is specified, in which case changes are lost.
The file remains unaffected.  Any windows for this buffer are
closed.  If buffer [N] is the current buffer, another buffer
will be displayed instead.  This is the most recent entry in
the jump list that points into a loaded buffer.
Actually, the buffer isn't completely deleted, it is removed
from the buffer list |unlisted-buffer| and option values,
variables and mappings/abbreviations for the buffer are
cleared.

Solution 5 - Vim

If you've saved the last file already, then :enew is your friend (:enew! if you don't want to save the last file). Note that the original file will still be in your buffer list (the one accessible via :ls).

Solution 6 - Vim

If you modify a file and want to close it without quitting Vim and without saving, you should type :bd!.

Solution 7 - Vim

:bd can be mapped. I map it to F4, Shift-F4 if I need to force-close because of some change I no longer want.

Solution 8 - Vim

The bufkill.vim plugin adds BD etc., to delete the buffer without closing any splits (as bd) would alone.

Solution 9 - Vim

Insert the following in your .vimrc file and, with pressing F4, it will close the current file.

:map <F4> :bd<CR>

Solution 10 - Vim

Look at the Butane plugin to keep the window layout when closing a buffer.

Solution 11 - Vim

For me close-buffers is pretty useful plugin:

:Bdelete this to close that you can remap.

Solution 12 - Vim

I have the same issue so I made the plugin. This plugin replace :q and other commands and then prevent the window closed.

if you still have issue, please try to use following plugin. https://github.com/taka-vagyok/prevent-win-closed.vim

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid.Chu.caView Question on Stackoverflow
Solution 1 - VimVinko VrsalovicView Answer on Stackoverflow
Solution 2 - VimsebnowView Answer on Stackoverflow
Solution 3 - VimGowriView Answer on Stackoverflow
Solution 4 - VimephemientView Answer on Stackoverflow
Solution 5 - VimRytmisView Answer on Stackoverflow
Solution 6 - VimpablofiumaraView Answer on Stackoverflow
Solution 7 - VimwbogaczView Answer on Stackoverflow
Solution 8 - VimblueyedView Answer on Stackoverflow
Solution 9 - VimStrykerView Answer on Stackoverflow
Solution 10 - VimGary WilloughbyView Answer on Stackoverflow
Solution 11 - VimJoannesView Answer on Stackoverflow
Solution 12 - VimTakashi KojimaView Answer on Stackoverflow