tags:

views:

8025

answers:

5

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.

+6  A: 

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

Rytmis
+43  A: 

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

:bd
Vinko Vrsalovic
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
+1, you beat me to it!
ephemient
When I do this, vim shows the first buffer, but I can still access the buffer
Martin Andersson
+5  A: 
:[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.
ephemient
+10  A: 

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
Gowri
Now what this snippet does?
dolzenko
+3  A: 

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

wbogacz