ChatGPT解决这个技术问题 Extra ChatGPT

Close file without quitting VIM application?

vim

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.


V
Vinko Vrsalovic

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

:bd 

Yours is better than mine for what the OP asked, although I tend to prefer :enew because I like having the buffer in the buffer list. :)
When I do this, vim shows the first buffer, but I can still access the buffer
@Martin, to completely delete the buffer, use :bw
t
the Tin Man

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.


Just keep in mind that VIM's help says this about :bw -- "Don't use this unless you know what you are doing."
t
the Tin Man

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

Now what this snippet does?
You probably want after the calls to actually execute the command.
@dolzenko it maps 'fc' and 'fq' in normal mode to optionally save the current buffer then switch to a new buffer before deleting the original. This preserves any splits you have set up.
Seems like a good idea. When I try it, though, I get the following error message: "Cannot redefine function CleanClose: It is in use"
Don't remap "f" . . . "f" is actually super useful. With this mapping, you can never "find" forward for "c" or "q."
e
ephemient
:[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.

t
the Tin Man

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).


t
the Tin Man

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


t
the Tin Man

: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.


t
the Tin Man

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


t
the Tin Man

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

:map <F4> :bd<CR>

t
the Tin Man

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


J
Joannes

For me close-buffers is pretty useful plugin:

:Bdelete this to close that you can remap.


T
Takashi Kojima

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