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.
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.
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.
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 ).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With