Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo all changes since opening buffer in vim

Tags:

vim

editor

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.

like image 644
David Rivers Avatar asked Dec 19 '11 17:12

David Rivers


People also ask

How do I undo all changes in vi?

Undo Changes in Vim / Vi To undo changes in Vim and Vi use the u , :u or :undo commands: If you are in insert or any other mode, press the Esc key to go back to the normal mode, which is also known as command mode. Type u to undo the last change. In Vim, the u command also accepts quantifiers.

How do I undo a previous change in Vim?

Remember, yo undo a change in Vim / Vi use the command u , and to redo a change which was undone use Ctrl-R . Once you have finished, make sure to save the Vim file before exiting.

How do I close current buffer in Vim?

Close buffer named Name (as shown by :ls ). Assuming the default backslash leader key, you can also press \bd to close (delete) the buffer in the current window (same as :Bclose ).

What is the command to undo what you just did in vi?

Undoing the Previous Command If you make a mistake in vi or if you just change your mind after an operation is completed, you can undo your last command by pressing u immediately after the command. You do not need to press Esc after you type u . By pressing u a second time you undo the undo.


1 Answers

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 
like image 91
ib. Avatar answered Sep 30 '22 17:09

ib.