HOWTO · Linux

How to Perform Undo and Redo Operations in Vim and Vi

Learn how to undo and redo changes in Vim and Vi, use `:undolist` and `:earlier` to navigate undo history, and understand undo grouping.

On this page

In Vim, return to Normal mode with Esc, then press u to undo the latest change or Ctrl-R to redo a change you just undid. These are Normal-mode keys. Ex commands such as :undo and :redo are entered on Vim’s command line after :.

Undo Changes in Vim and Vi

Press Esc to enter Normal mode, then press u. To undo several changes, use a count such as 3u or press u repeatedly. The Ex forms :u and :undo also undo a change; type the colon as part of the command.

For example, start with Hello World. In Insert mode, add Beautiful between the words, then press Esc; the line becomes Hello Beautiful World. Press u in Normal mode to restore Hello World. Undo groups depend on Vim’s editing rules and context, so a stretch of typing is not guaranteed to form one undo step in every situation.

The small shell example below models the same before, edited, undone, and redone text states. It illustrates the text sequence; the Vim keystrokes above are performed in the editor.

before='Hello World'
edited='Hello Beautiful World'
current=$edited
printf 'Edited: %s\n' "$current"
current=$before
printf 'After u: %s\n' "$current"
current=$edited
printf 'After Ctrl-R: %s\n' "$current"
Edited: Hello Beautiful World
After u: Hello World
After Ctrl-R: Hello Beautiful World

Redo Changes in Vim and Vi

After undoing, press Ctrl-R in Normal mode to redo the change. A count such as 3<C-R> redoes up to three undone changes. You can also enter :redo on the command line. In the example above, Ctrl-R restores Hello Beautiful World.

Redo follows the current undo history. If you undo and then make a new edit, Vim changes that history; do not expect Ctrl-R to restore an abandoned branch of edits.

Use :undolist to display undo sequence information. To move to a numbered state, use :undo {N}; {N} is an undo sequence number, not an arbitrary change ID. For example, :undo 3 moves to sequence number 3.

Use :earlier {N} to move back by {N} changes; for example, :earlier 3 moves back three changes. The Normal-mode command g- moves to older text states, while g+ moves toward newer states. These commands navigate Vim’s undo history; they do not select entries by the numbers printed by :undolist.

Understand Undo Blocks and the U Command

Lowercase u undoes a change in the undo history. Uppercase U has a different, line-focused behavior: in Vim it restores the current line to its earlier state according to Vim’s undo rules. It is not another spelling of u, and it does not mean “undo every change on the line” in all editing contexts. Undo grouping can vary with the commands used and when Vim records a change.

Vi implementations differ, so check the help for the editor you are using if a command behaves differently. Vim’s undo history is normally available while that editing session remains open. Across sessions, persistence depends on Vim’s undo-file configuration; saving with :w writes the buffer but does not by itself guarantee that undo history will persist.

Common Questions

How do I undo multiple changes? Return to Normal mode and use a count such as 3u, or repeat u.

What is the redo key? Press Ctrl-R in Normal mode, or enter :redo on Vim’s command line.

How can I inspect or navigate undo history? :undolist lists sequence information; :undo {N} targets a sequence number. :earlier 3 moves back three changes, while g- and g+ move between older and newer states.

Does Vim keep undo history after closing? It can when persistent undo is configured and the undo file is available. Without that setup, do not assume session history will survive closing Vim. :w saves your file contents, not a promise of persistent undo history.