Persistent undo and force quit

61 views
Skip to first unread message

David Besen

unread,
Jul 20, 2015, 6:44:41 PM7/20/15
to v...@vim.org
I have persistent undo turned on.  I noticed this behavior:

- If I edit a file, make a change and don't save it, execute :qall!, edit the file again, and try to redo, the change is gone.

- If I edit a file, make a change, undo it, save the file (essentially making no change to the file on disk), quit, edit the file again, and try to redo, the change is made!  The undo tree was saved even though there was no change to the file.

I would like to get vim to save the undo history even if I make an edit and then execute :qall!.  Is there any way to do that?

- Dave

Justin M. Keyes

unread,
Jul 22, 2015, 1:17:16 PM7/22/15
to vim_use, v...@vim.org

Seems like a bug. But to work around it you could :wundo before :qall!

Justin M. Keyes

David Besen

unread,
Jul 22, 2015, 3:34:48 PM7/22/15
to v...@vim.org

I tried that and it didn't work.  First, :wundo needs a filename, so I gave it the name of the current undo file.  And then it didn't save the change I made and undid.

- Dave

Christian Brabandt

unread,
Jul 27, 2015, 4:07:50 PM7/27/15
to v...@vim.org
Hi David!
undo files are only written, when the buffer is written. If you abort
editing, there is no reason to write something.

My guess is, it is avoided to make aborting work faster
since for writing the undo file, a hash of the buffer has to be
calculated and then the undo file needs to be written, which also might
take some time.

Now imagine several large buffers and a slow network file system.

You could possibly script something using the mentioned wundo command
and a VimLeave autocommand.

Best,
Christian
--
Eine Million Steuerzahler verhalten sich vernünftiger als eine
öffentliche Hand.
-- Hermann Josef Abs

David Besen

unread,
Jul 28, 2015, 1:18:55 PM7/28/15
to v...@vim.org
Here's a first draft of getting it to work:

"save undo if we qa!
function! MyWundoQuit()
    silent! later 99999
    earlier 1f
    let undof = escape(undofile(expand('%')),'% ')
    exec "wundo " . undof
endfunction

autocmd BufWinLeave * call MyWundoQuit()


I don't see a way to undo back to when the file was written last other than going later, and then earlier 1f.  That doesn't work in the case that the file hasn't been changed at all, so it's not ideal.  But, it seems to work in practice pretty well.

- Dave

David Besen

unread,
Jul 28, 2015, 6:31:41 PM7/28/15
to v...@vim.org
Second draft, seems to work a little better:

"save undo if we qa!
function! MyWundoQuit()
    let undoseq = undotree().seq_cur
    earlier 1f
    let undof = escape(undofile(expand('%')),'% ')
    exec "wundo " . undof
    silent! exec "u " . undoseq

Justin M. Keyes

unread,
Jul 30, 2015, 6:27:59 PM7/30/15
to vim_use

Why does the second draft still have "earlier 1f" ? Why can't you just write the undo file?

David Besen

unread,
Jul 31, 2015, 1:25:35 PM7/31/15
to v...@vim.org

> On Jul 28, 2015 18:31, "David Besen" <[hidden email]> wrote:
> >
> > Second draft, seems to work a little better:
> >
> > "save undo if we qa!
> > function! MyWundoQuit()
> >     let undoseq = undotree().seq_cur
> >     earlier 1f
> >     let undof = escape(undofile(expand('%')),'% ')
> >     exec "wundo " . undof
> >     silent! exec "u " . undoseq
> > endfunction
> >
> > autocmd BufWinLeave * call MyWundoQuit()

> Why does the second draft still have "earlier 1f" ? Why can't you just write the undo file?

Without it, the "redo" after reloading the file doesn't work.  I don't know why not -- if I revert to how the file was before wundo, then it works.

- Dave
Reply all
Reply to author
Forward
0 new messages