How to autosave files when terminal is closed

24 views
Skip to first unread message

Dennis Nezic

unread,
Oct 24, 2022, 2:42:13 AM10/24/22
to vim...@googlegroups.com
"au VimLeavePre * w" only works when I exit vim with something like
":q!" but not if I close my terminal window.

Error detected while processing VimLeavePre Autocommands for "*":
E749: Empty buffer

VimLeave[Pre] is being called, but the buffer is not saved, probably
because it's closed before that event is triggered. Is there another
event that gets triggered during the closing sequence that will allow
me to autosave, or another way to do this?

Owajigbanam Ogbuluijah

unread,
Oct 24, 2022, 5:31:27 AM10/24/22
to vim...@googlegroups.com
Hi Dennis,

Are you asking for an event which fires when you close the terminal within Vim — opened with :termor the terminal window that's housing Vim?

If it's the later, I wonder if SigUSR1 may help here? — where SigUSR1 is the interrupt your terminal sends to Vim in the event that it's being closed impromptu — disclaimer: I haven't tried this; I'm just inferring from the docs:h autocmd-events

If you want to autosave, there's a nifty battle-tested Vim plugin — vim-auto-save — which I've entrusted to doing this for me. 


Best,
Igbanam.

--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20221023224251.eaf04689726b89d749e73d9b%40dennisn.mooo.com.

Dennis Nezic

unread,
Oct 24, 2022, 6:18:19 PM10/24/22
to vim...@googlegroups.com
SigUSR1 didn't work. I wonder why SigTERM isn't in autocmd-events.

That plugin is a bit overkill (vim already stores unsaved changes in
swap files), and hooks into events like CursorHold (cursor inactivity)
and CompleteDone.

Why isn't this possible in VimLeavePre (if not VimLeave)? :|

On Mon, 24 Oct 2022 09:16:55 +0000, Owajigbanam Ogbuluijah wrote:
> Hi Dennis,
>
> Are you asking for an event which fires when you close the terminal
> within Vim — opened with *:term* — *or* the terminal window that's
> housing Vim?
>
> If it's the later, I wonder if SigUSR1 may help here? — where SigUSR1
> is the interrupt your terminal sends to Vim in the event that it's
> being closed impromptu — *disclaimer: I haven't tried this; I'm just
> inferring from the docs* — *:h autocmd-events*
>
> If you want to autosave, there's a nifty battle-tested Vim plugin —
> vim-auto-save <https://github.com/vim-scripts/vim-auto-save> — which
> https://groups.google.com/d/msgid/vim_use/CAOmRJreOa6xqQ6%3D%2BU8zX7D3N7DpXfrJ4nPVaoP3yGzjNMoLaUQ%40mail.gmail.com.

Bram Moolenaar

unread,
Oct 28, 2022, 5:23:45 PM10/28/22
to vim...@googlegroups.com, Dennis Nezic
Do you already have the 'autowriteall' option set?

--
Mrs Abbott: I'm a paediatrician.
Basil: Feet?
Mrs Abbott: Children.
Sybil: Oh, Basil!
Basil: Well, children have feet, don't they? That's how they move
around, my dear. You must take a look next time, it's most
interesting. (Fawlty Towers)

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Dennis Nezic

unread,
Oct 28, 2022, 5:41:37 PM10/28/22
to Bram Moolenaar, vim...@googlegroups.com
On Fri, 28 Oct 2022 22:23:31 +0100, Bram Moolenaar wrote:
>
> > "au VimLeavePre * w" only works when I exit vim with something like
> > ":q!" but not if I close my terminal window.
> >
> > Error detected while processing VimLeavePre Autocommands for "*":
> > E749: Empty buffer
> >
> > VimLeave[Pre] is being called, but the buffer is not saved, probably
> > because it's closed before that event is triggered. Is there another
> > event that gets triggered during the closing sequence that will
> > allow me to autosave, or another way to do this?
>
> Do you already have the 'autowriteall' option set?

I think that only affects closing via :quit and the like. Closing my
xterm apparently doesn't follow that same exit pathway.

Bram Moolenaar

unread,
Oct 28, 2022, 7:03:39 PM10/28/22
to vim...@googlegroups.com, Dennis Nezic
It is supposed to, but perhaps the catching of signals is not properly
setup. Not sure how to try it out other than closing the xterm
normally. If you "kill -9" the xterm it might not work. Closing the
window manager should also work OK. At least this mechanism is used to
avoid leaving swap files laying around, and that works quite well, thus
'autowriteall' should also work. But I don't think there is a test for
it.

--
A village. Sound of chanting of Latin canon, punctuated by short, sharp
cracks. It comes nearer. We see it is a line of MONKS ala SEVENTH SEAL
flagellation scene, chanting and banging themselves on the foreheads with
wooden boards.
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

Dennis Nezic

unread,
Oct 28, 2022, 11:23:13 PM10/28/22
to Bram Moolenaar, vim...@googlegroups.com
On Sat, 29 Oct 2022 00:03:17 +0100, Bram Moolenaar wrote:
>
> > On Fri, 28 Oct 2022 22:23:31 +0100, Bram Moolenaar wrote:
> > >
> > > > "au VimLeavePre * w" only works when I exit vim with something
> > > > like ":q!" but not if I close my terminal window.
> > > >
> > > > Error detected while processing VimLeavePre Autocommands for
> > > > "*": E749: Empty buffer
> > > >
> > > > VimLeave[Pre] is being called, but the buffer is not saved,
> > > > probably because it's closed before that event is triggered. Is
> > > > there another event that gets triggered during the closing
> > > > sequence that will allow me to autosave, or another way to do
> > > > this?
> > >
> > > Do you already have the 'autowriteall' option set?
> >
> > I think that only affects closing via :quit and the like. Closing my
> > xterm apparently doesn't follow that same exit pathway.
>
> It is supposed to, but perhaps the catching of signals is not properly
> setup. Not sure how to try it out other than closing the xterm
> normally. If you "kill -9" the xterm it might not work. Closing the
> window manager should also work OK. At least this mechanism is used
> to avoid leaving swap files laying around, and that works quite well,
> thus 'autowriteall' should also work. But I don't think there is a
> test for it.

Yea, I guess signal catching isn't properly setup. It seems that my wm
is sending vim a SIGHUP when it closes the xterm window:

Vim: Caught deadly signal HUP
Vim: preserving files...
Vim: Finished.

Manually killing vim with "kill (-1|-3|-15)" does not trigger that nice
autowriteall (awa) feature, even though vim's output says "preserving
files..."

Bram Moolenaar

unread,
Oct 29, 2022, 8:05:00 AM10/29/22
to vim...@googlegroups.com, Dennis Nezic
I suppose TERM and HUP are handled differently. But that is logical,
I'm not sure we should handle them the same way. 'autowriteall' is
boolean, not sure what would be a good way to make another choice of how
to handle TERM. Well, maybe we can just change it, for most users they
will do the same thing anyway, just come from different causes (that you
don't have control over).

--
FIRST VILLAGER: We have found a witch. May we burn her?
Reply all
Reply to author
Forward
0 new messages