Undo


Vim comes with a very powerful built-in undo engine. While most editors support a linear undo/redo, where all changes are tracked sequentially, Vim maintains a tree of changes. For now, we will describe basic undo and redo operations, which make up the majority of undo operations, and save the more complicated operations for a future section.

Let's demonstrate the basics with the following buffer:

Initial buffer content
one
two
three
four
five
six
seven
eight
NORMALTop1:1
 

First, make some edits. For example, lets delete 4 lines:

Make an editd4d
five
six
seven
eight
NORMALTop1:1
4 fewer lines

Now, that we have made a change, we can undo the most recent change by typing u. The u command also takes a count, so if you want to undo the last 3 changes you can type 3u.

Undo the editu
one
two
three
four
five
six
seven
eight
NORMALTop1:1
4 more lines; before #1 0 seconds ago

If you prefer to use command-line mode, you can achieve the same result with the :undo command:

:undo [count]

Now, let's redo the change, using C-R

Redo the edit<C-r>
five
six
seven
eight
NORMALTop1:1
4 fewer lines; after #1 0 seconds ago

C-R also accepts an optional count, and the command-line option is:

:redo [count]

Check back soon to learn more about more advanced undo and redo operations in Vim.