Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close file without quitting VIM application?

Tags:

vim

People also ask

How do I save in VI without exiting?

The command to save a file in Vim is :w . To save the file without exiting the editor, switch back to normal mode by pressing Esc , type :w and hit Enter . There is also an update command :up , which writes the buffer to the file only if there are unsaved changes.

How do I save and exit from Vim editor?

To save a file in Vim / vi, press Esc key, type :w and hit Enter key. One can save a file and quit vim / Vi by pressing Esc key, type :x and hit Enter key.

Which Vim command will save the current file without exiting?

Type w after the colon and hit Enter. This will save in Vim the changes made to the file, without exiting.

How do you close a file in Linux?

They will disappear as your typing extends down the screen. To close a file to which no changes have been made, hit ESC (the Esc key, which is located in the upper left hand corner of the keyboard), then type :q (a colon followed by a lower case "q") and finally press ENTER.


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

:bd 

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.


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

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

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