Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quit vim, when a buffer has unsaved changes and is un-named?

Tags:

vim

editor

I created a blank "scratch buffer" (i.e. not associated with a specific file) in vim, by using :vnew. Then I played around with some text, now I simply want to exit the editor - not keeping the contents of this "scratch buffer".

When I type the command:

:q!

Vim gives me:

[No Name][+]                                               
E37: No write since last change (add ! to  override)  
E162: No write since last change for buffer "[No Name]"  
Press ENTER or type command to continue

How can I quit vim from this state?

like image 811
the_velour_fog Avatar asked Aug 11 '15 07:08

the_velour_fog


People also ask

How do I force quit from vim?

TL;DR – How to Exit Vim If you didn't make any changes, type :q and press Enter / return. If you made some changes and would like to keep them, type :wq and press Enter / return. If you made some changes and would rather discard them, type :q! and press Enter / return.

How do I stop vim from saving and quitting?

Quit Vim / Vi without Saving the File To exit the editor, without saving the changes, switch to normal mode by pressing Esc , type :q! and hit Enter .

What keyboard combination will save and quit the file with vim?

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.


2 Answers

This happens when you have a modified hidden buffer. Use :qa! to exit anyway.

From :h :quit :

:q[uit]!        Quit without writing, also when currently visible
                buffers have changes.  Does not exit when this is the
                last window and there is a changed hidden buffer.
                In this case, the first changed hidden buffer becomes
                the current buffer.
                Use ":qall!" to exit always.

In case someone wants to reproduce it:

  • Start vim and modify the unnamed buffer
  • Edit another file (you might need to :set hidden), ie :e README
  • Try to exit with :q!
like image 75
Marth Avatar answered Oct 24 '22 09:10

Marth


At best you could call it a "transient" buffer (in memory but not associated with a specific file) but what you created with :vnew is not a "scratch" buffer.

In Vim, a "scratch" buffer is a normal buffer in which you set a bunch of local options. It can be done with a command like this:

:vnew | setlocal nobuflisted buftype=nofile bufhidden=wipe noswapfile
like image 6
romainl Avatar answered Oct 24 '22 07:10

romainl