-1

I often open Vim to take some quick notes about something or pipe in output from another program for quick processing.

99% of the time I don't want to keep these files and want them to close automatically with :q instead of using :q!.

Ideally however, if I save this data to a file and make more changes, I want to be warned if I try to quit without saving. Is there a way to set up vim to work this way?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303

2 Answers2

2

You can stick this into your vimrc:

set hidden
command! -bang QALL execute "bufdo if expand('%')=='' | setlocal buftype=nofile | endif" | qall<bang>
command! -bang WQALL execute "bufdo if expand('%')=='' | setlocal buftype=nofile | endif" | wqall<bang>

Now :QA does what the user should have done all along: sets buffers intended as scratch buffers as scratch buffers. After that, qall (or qall!, if you do :QA!) does the right thing. Same with :WQA and :WQA!.

See :help special-buffers, :help 'buftype'.

Personally, I like to be explicit about these things, marking buffers as scratch when I need a scratch buffer, for fear I'll lose something I actually wanted to save: :setl bt=nofile

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • 1
    "I like to be explicit...", yes. Same here. I'd rather be aware of what the editor is going to do when I have a bunch of buffers and windows open, rather than let Vim run on cruise-control and do the wrong thing. I think that caution is the result of being burned a couple times and losing work inadvertently. – the Tin Man Nov 26 '19 at 19:34
1

It's always a good idea to read the documentation many times. From your description it sounds like Vim already does what you want, you just have to tell it to do it:

:q[uit]     Quit the current window.  Quit Vim if this is the last
            window.  This fails when changes have been made and
            Vim refuses to |abandon| the current buffer, and when
            the last file in the argument list has not been
            edited.
:q[uit]!    Quit without writing, also when the current buffer has
            changes.  The buffer is unloaded, also when it has
            'hidden' set.

and

:[range]x[it][!] [++opt] [file]
            Like ":wq", but write only when changes have been
            made.
            When 'hidden' is set and there are more windows, the
            current buffer becomes hidden, after writing the file.

Similarly, you can write if the file has changed and quit:

ZZ          Write current file, if modified, and quit (same as
            ":x").  (Note: If there are several windows for the
            current file, the file is written if it was modified
            and the window is closed).

Finally, this is significant:

Vim remembers whether you have changed the buffer.  You are protected from
losing the changes you made.  If you try to quit without writing, or want to
start editing another file, Vim will refuse this.  In order to overrule this
protection, add a '!' to the command.  The changes will then be lost.  For
example: ":q" will not work if the buffer was changed, but ":q!" will.  To see
whether the buffer was changed use the "CTRL-G" command.  The message includes
the string "[Modified]" if the buffer has been changed.

If you want to automatically save the changes without asking, switch on the
'autowriteall' option.  'autowrite' is the associated Vi-compatible option
that does not work for all commands.

"What is your most productive shortcut with Vim?" will probably be useful too.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • I appreciate your response, though I think you've missed what I'm asking (and I think I could have phrased it better). What I would like is for the buffers that are not associated with a file to not prevent closure when they aren't written to. For example, if I have several buffers or tabs and one is a notpad without a file that it can write to, I want commands that close buffers like ```qall``` to not fail due to unsaved changes. However, if a buffer is associated with a file, I don't want it to close. That is the specific behavior I am wondering about. – Samuel Jackson Nov 26 '19 at 00:48
  • Perhaps the [Vim beta site](https://vi.stackexchange.com/) would be better suited. – the Tin Man Nov 26 '19 at 19:32