How to undo up to last write in vim?

You can get all the way back to when you first opened the file pretty easily. Just type a number before u.

10000u, will undo 10000 times. If that's not enough try 1000000u :)

If you want to undo bit by bit, you can do it in any increment, try 5u.

If you just want to reload the file from disk use :e.


From Vim's help:

:earlier {N}f   Go to older text state {N} file writes before.
                When changes were made since the last write
                ":earlier 1f" will revert the text to the state when
                it was written.  Otherwise it will go to the write
                before that.
                When at the state of the first file write, or when
                the file was not written, ":earlier 1f" will go to
                before the first change.

So, if you didn't make changes after the second save, you can do what you want with:

:earlier 1f

On the other hand, if you did unsaved changes after the second save, then:

:earlier 2f

will solve your problem.

See :help :earlier, :help :undolist.

Tags:

Vim