David.Chu.ca
David.Chu.ca

Reputation: 38684

Close file without quitting VIM application?

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.

Upvotes: 287

Views: 113231

Answers (12)

Joannes
Joannes

Reputation: 2948

For me close-buffers is pretty useful plugin:

:Bdelete this to close that you can remap.

Upvotes: 0

Gary Willoughby
Gary Willoughby

Reputation: 52608

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

Upvotes: 0

Stryker
Stryker

Reputation: 6130

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

:map <F4> :bd<CR>

Upvotes: 3

blueyed
blueyed

Reputation: 27878

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

Upvotes: 4

wbogacz
wbogacz

Reputation: 199

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

Upvotes: 4

pablofiumara
pablofiumara

Reputation: 1797

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

Upvotes: 8

Rytmis
Rytmis

Reputation: 32067

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

Upvotes: 11

Gowri
Gowri

Reputation: 1331

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

Upvotes: 25

sebnow
sebnow

Reputation: 1380

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.

Upvotes: 54

Takashi Kojima
Takashi Kojima

Reputation: 1

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

Upvotes: -1

ephemient
ephemient

Reputation: 204964

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

Upvotes: 18

Vinko Vrsalovic
Vinko Vrsalovic

Reputation: 340456

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

:bd 

Upvotes: 401

Related Questions