Close file without quitting VIM application?

Solution 1:

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

:bd 

Solution 2:

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:

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