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.
This deletes the buffer (which translates to close the file)
:bd
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.
:bw
-- "Don't use this unless you know what you are doing."
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
:[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.
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
).
If you modify a file and want to close it without quitting Vim and without saving, you should type :bd!
.
: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.
The bufkill.vim plugin adds BD
etc., to delete the buffer without closing any splits (as bd
) would alone.
Insert the following in your .vimrc file and, with pressing F4, it will close the current file.
:map <F4> :bd<CR>
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
Success story sharing