Undo all changes since opening buffer in vim

VimEditor

Vim Problem Overview


How can I undo all changes since opening a buffer? I imagine there may be some form of :earlier that does this.

UPDATE: Many are suggesting solutions for traversing to earlier file writes. This isn't what I asked for. I want to return to the original state the file was in when I originally loaded it into a buffer, no matter how many writes were made since then.

Vim Solutions


Solution 1 - Vim

To revert the current buffer to the original state prior to the very first change recorded in its undo list (see :help undo-tree), one can use the following two consecutive invocations of the :undo command:

:u1|u

The first command (:undo 1) reverts to the state of the buffer just after the very first registered change, while the second command (:undo) reverts that first change itself.

Starting with version 8.1 (see :helpg Patch 8.0.1441), Vim accepts the change number 0 as a valid argument to the :undo command, finally providing a way to refer to the state prior to any registered changes. This makes it possible to achieve the same effect in a single-command invocation:

:u0

Solution 2 - Vim

You can use the

:edit!

command to get into the earliest saved state. See :help edit! for more information.

You can also check something like gundo.vim (can be found here), which displays the whole undo tree graphically, and you can easily jump between points. Then there is the histwin plugin which I did not used yet, but offers similar functionality.

Solution 3 - Vim

In vim 8.1+ as well as in neovim, you can just use :u0

Solution 4 - Vim

From the documentation > :u[ndo] {N} Jump to after change number {N}. See |undo-branches| for the meaning of {N}. {not in Vi}

If you type :u 1 it appears to go to after the first change; pressing u or typing :u will then go back to the change.

Otherwise, you can use a very large count to :earlier or g- e.g. :earlier 100000000 or 100000000g-

If you put this into a mapping/command, it could do any of these without too much trouble. e.g.

:nnoremap <C-F12> :earlier 100000000<CR>

Solution 5 - Vim

To access previously saved file status, I think the following work :

:earlier 1f

From the documentation :

 :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.

Solution 6 - Vim

:earlier {N}m Go to older text state about {N} minutes before.

That should help... And even you have {N}h which is about {N} hours before.

Solution 7 - Vim

A graphic solution:

The Gundo plugin allows visual comparison of changes in the undo history.

Open Gundo's "undo history pane", type G go to the last line, then we can back to the original file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid RiversView Question on Stackoverflow
Solution 1 - Vimib.View Answer on Stackoverflow
Solution 2 - VimZsolt BotykaiView Answer on Stackoverflow
Solution 3 - VimHopeView Answer on Stackoverflow
Solution 4 - VimbenpmorganView Answer on Stackoverflow
Solution 5 - VimXavier T.View Answer on Stackoverflow
Solution 6 - Vimuser379997View Answer on Stackoverflow
Solution 7 - Vimying17ziView Answer on Stackoverflow