Unintentionally closing Vim with many buffers open

1,020 views
Skip to first unread message

wolfv

unread,
Dec 5, 2015, 2:18:02 PM12/5/15
to vim_use
Sometimes I unintentionally close Vim with many buffers open.
Then I have to reopen Vim and reopen all the buffers I was working on.
Is there a way to prevent this, like a plugin that prompts "Multiple buffers are open, are you sure you want to close them?"

Thank you.

Random832

unread,
Dec 5, 2015, 2:59:46 PM12/5/15
to vim...@googlegroups.com
How exactly do you create a session that fails to do this? I'm
not exactly familiar with all the ways to work with hidden
buffers, but I do know that in the most common case (multiple
files passed in on the command line), you get E173 if you try to
quit before editing all of them, and E162 if 'hidden' was
enabled and any of the others were unwritten.

Do you want it to refuse to quit even if the buffers aren't
unwritten? Do you want it to prompt even if you've typed "qa!"?

Wolfram Volpi

unread,
Dec 5, 2015, 3:13:14 PM12/5/15
to vim...@googlegroups.com
Hi Random832,
All the buffers are all written.  Maybe "open" was not the right word.  I use the buffer list as a conveniently access the files I am working on.


--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to a topic in the Google Groups "vim_use" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_use/HCNPrkw9Wg8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vim_use+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
It's bad luck to be superstitious.

Wolfram Volpi
telephone: 575-323-1432
e-mail: wolf...@gmail.com

lith

unread,
Dec 6, 2015, 2:07:28 AM12/6/15
to vim_use
> Then I have to reopen Vim and reopen all the buffers I was working on.

Check out :help views-sessions

There are some session manager plugins around on vim.org that do exactly what you want. I personally prefer the "most recently used files" though -- like (shameless plug) tmru_vim.

HTH

Ben Fritz

unread,
Dec 7, 2015, 12:37:14 AM12/7/15
to vim_use

Why are you using :q if you want Vim to stay open? Maybe :bd instead would suit your purposes better?

wolfv

unread,
Dec 7, 2015, 2:28:26 AM12/7/15
to vim_use

I didn't explain very well. This should clarify:

I usually work with one primary Vim open with multiple files in the buffers list.
Sometimes I will also temporarily open a few text files from the file manager, each file opened in its own Vim.
Then I quickly close all the temporary Vims by clicking the x in the top-right corner.
But sometimes I unintentionally close the primary Vim with the many files in the buffers list.
Then I have to reopen Vim and reopen all the files I was working on.
Is there a way to stop me from unintentionally closing Vim when the buffer list contains multiple files, like a configuration or plugin that prompts "Multiple files in buffers list, are you sure you want to close Vim?"

Some session managers give such a warning message.
But I don't want a session manager, I just the warning message.

I use ":source .fileList" to open groups of files that are frequently opened.
But that doesn't help when I close Vim with a one-off group of buffers.

Christian Brabandt

unread,
Dec 7, 2015, 4:11:44 AM12/7/15
to vim_use
Hi wolfv!

On So, 06 Dez 2015, wolfv wrote:

> I usually work with one primary Vim open with multiple files in the
> buffers list. Sometimes I will also temporarily open a few text files
> from the file manager, each file opened in its own Vim. Then I quickly
> close all the temporary Vims by clicking the x in the top-right
> corner. But sometimes I unintentionally close the primary Vim with the
> many files in the buffers list. Then I have to reopen Vim and reopen
> all the files I was working on. Is there a way to stop me from
> unintentionally closing Vim when the buffer list contains multiple
> files, like a configuration or plugin that prompts "Multiple files in
> buffers list, are you sure you want to close Vim?"

Just always keep a modified buffer around. This can possibly be a hidden
scratch buffer, whose sole purpose is to prevent a quick exit.


Best,
Christian
--
Wenn der moderne Mensch die Tiere, deren er sich als Nahrung bedient,
selbst töten müsste, würde die Anzahl der Pflanzenesser ins
Ungemessene steigen.
-- Christian Morgenstern

Tony Mechelynck

unread,
Dec 7, 2015, 7:21:47 AM12/7/15
to vim...@googlegroups.com
On Mon, Dec 7, 2015 at 10:11 AM, Christian Brabandt <cbl...@256bit.org> wrote:
> Hi wolfv!
>
> On So, 06 Dez 2015, wolfv wrote:
>
>> I usually work with one primary Vim open with multiple files in the
>> buffers list. Sometimes I will also temporarily open a few text files
>> from the file manager, each file opened in its own Vim. Then I quickly
>> close all the temporary Vims by clicking the x in the top-right
>> corner. But sometimes I unintentionally close the primary Vim with the
>> many files in the buffers list. Then I have to reopen Vim and reopen
>> all the files I was working on. Is there a way to stop me from
>> unintentionally closing Vim when the buffer list contains multiple
>> files, like a configuration or plugin that prompts "Multiple files in
>> buffers list, are you sure you want to close Vim?"
>
> Just always keep a modified buffer around. This can possibly be a hidden
> scratch buffer, whose sole purpose is to prevent a quick exit.
>
>
> Best,
> Christian

Also, don't close Vim by clicking the [X] button at top right (in the
OS-provided window decorations). Using one of the following gives you
more control:
:qa! " close Vim and discard all changes
:qa " close Vim, but not if there are changes
:xa " close Vim and save all changes
(where q stands for "quit", x stands for "exit", and a stands for "all")
The latter one will also error out if there exists a modified file
which cannot be saved, usually because it hasn't yet been named. So if
you keep one or more temporary unnamed buffer(s) with, let's say,
stuff input by means of digraphs, keymaps, etc. in order to feed it to
another program by means of the clipboard (typical temporary stuff for
me), that Vim won't be "accidentally killed": you'll have to either
use :qa! with bang (and better triple-check what you are closing
before you use that one, because it cannot be undone), or close that
particular temporary buffer with :q! (which closes nothing else,
except possibly a help buffer if these are the only two windows left,
and when you use that command you see the file being closed, it is in
the current window).

Best regards,
Tony.

Michael Henry

unread,
Dec 7, 2015, 7:38:34 AM12/7/15
to vim...@googlegroups.com
On Mon, Dec 7, 2015 at 10:11 AM, Christian Brabandt <cbl...@256bit.org>
wrote:
> Just always keep a modified buffer around. This can possibly
> be a hidden scratch buffer, whose sole purpose is to prevent a
> quick exit.

I wondered whether some kind of autocmd would help here. I
don't have time to check into it sufficiently myself, but I did
find this old thread from 2005 entitled "Using VimLeave to
Prevent Exiting":
http://vim.1045645.n5.nabble.com/Using-VimLeave-to-Prevent-Exiting-td1143011.html

The post states that the autocmd idea won't work when closing
Vim via the window's "X" close button. But maybe something has
changed in the past 10 years, so I thought I'd bring up the idea
just in case.

Michael Henry

Reply all
Reply to author
Forward
0 new messages