Monday, November 16, 2009

VIM: Close file without quiting VIM application?

VIM: Close file without quiting VIM application?

I am new to VIM. I use e and w commands to edit and to write a file. It is very convenient. I am not sure if there is "close" command to close the current file without leaving VIM?

I know that q command can be used to close a file. But if it is the last file, the VIM is closed as well(actually on Mac the MacVIM does quit. Only the VIM window is closed and I could use Control-N to open a blank VIM again). I would like the VIM to stay with a blank screen.

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


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. :) – Rytmis Nov 1 '08 at 22:39

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

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

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 :call CleanClose(1)

map fq :call CleanClose(0)

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

if (bufnr("%") == todelbufNr)
exe "bd".todelbufNr

No comments: