#

Editors

Subscribe
Editors October 29, 2026·5 min read

How to Undo and Redo in Vim

In Vim's Normal mode, press u to undo an accidental change and Ctrl+R to redo it if you went too far. If another edit created a branch or you reopened the file, recovery needs undo history that Vim still holds. Inspect the restored text before saving.

The basic keys

Vim undo commands

Press Esc if you're typing in Insert mode. Then use these Normal-mode keys:

What you wantKeysResult
Undo a changeuReverses the most recent change
Redo an undone changeCtrl+RRestores a change you just undid

For Ctrl+R, hold Ctrl and press R. You don't type the plus sign.

Try it in a scratch file containing a single line:

alpha

Append beta, then leave Insert mode. The text reads:

alpha beta

Press u. The buffer returns to alpha. Press Ctrl+R, and it reads alpha beta again.

Vim redo example

Tested in Vim 9.1 with the user configuration disabled.

Undo several changes at once

Vim undo blocks

Type 3u in Normal mode to undo three changes. Vim counts undo blocks, and one block can hold several characters or lines.

If you enter Insert mode and type hello world in one stretch, Vim records the insertion as one undo block, so a single u removes the entire phrase. Cursor movement while inserting, mappings, and deliberate breakpoints change the grouping.

To test it, make three separate edits, leaving Insert mode between them, then type 3u.

Vim change grouping

For finer control, press Ctrl+G followed by u while inserting. This Insert-mode undo breakpoint starts a new undo block, so separate stretches of typing undo separately. The Vim undo reference documents it.

How u, U, and dot differ

Vim command differences
  • u undoes a recorded change.
  • U reverses the changes on the last changed line, and that reversal is itself an undoable change. Say you start with lines alpha and beta and edit the first to alpha changed. Move the cursor to the untouched second line, and U acts on alpha, the last changed line.
  • The dot command (.) repeats the last repeatable editing operation. It is not redo.

Recover an older edit from Vim's undo tree

Vim undo branches

Undo an edit, then make a different one, and Vim creates another branch in its undo tree. The earlier text stays in the tree even though ordinary redo no longer follows that path.

Take this sequence, starting with alpha:

StepBuffer textWhat happened
StartalphaInitial text
First editalpha betaAdded beta
UndoalphaPressed u
Different editalpha gammaAdded gamma
Older branchalpha betaPressed g-
Newer branchalpha gammaPressed g+

In Normal mode, g- moves toward older text states and g+ toward newer ones, including states on alternate branches.

Vim branching history

One contributor to an r/vim undo-tree discussion (February 17, 2026) put it this way: “vim has a really nice undo state tree that can be navigated, though it can be a bit hard to track.”

Can you undo after saving or reopening a Vim file?

Vim saved changes
File stateUndo access
Edited and still openReachable while the change remains in session history
Saved with :w, still openWriting does not erase undo
Unloaded or closed, then reopenedEarlier session history is gone unless persistent undo data exists
Vim session history

A poster in an r/neovim undo-history thread (June 16, 2024) described it: “Whenever i save a file and exit , then reopen the file and try to undo it says Already on the latest change...”

Persistent undo

Vim persistent undo

With undofile enabled, Vim stores undo history for future editing sessions. The undodir setting specifies where it goes, and undolevels affects how much history Vim keeps. Before relying on an undo command after reopening a file, check whether persistent undo was configured. In-memory undo history is not the same as a backup.

You can return to the same file and reach its earlier edits.

Check the settings in your Vim session:

:set undofile?
:set undodir?
:set undolevels?
Vim undo settings

To enable the feature for later edits in that session, use:

:set undofile

Vim writes undo information when it writes the file. The stored history has to match the file when you edit it again. Enabling the setting after Vim has discarded earlier history does not bring those changes back.

To test cross-session recovery, use a scratch file: enable the option, make an edit, write the file, exit Vim, then reopen it and try to reach the earlier change.

When undo seems broken

Vim terminal suspension

In Vim's Normal or Visual mode, Ctrl+Z suspends the foreground Vim job in a Unix terminal with shell job control. Vim seems to vanish, though it neither quit nor undid your text.

In an r/vim Ctrl+Z thread on January 3, 2022, a beginner got stuck: “hey guys how do i get back to typing normally I am new to vim and I accidentally pressed control z to undo something but now I am stuck in this page” The poster later named Windows Terminal, PowerShell, and Neovim as the setup and said fg did not work there.

If a Unix shell with job control suspended your Vim session, run this at the shell prompt:

fg
Vim shell recovery

The shell brings the suspended job back to the foreground. fg is a shell command, so don't enter it as an Ex command inside Vim.

A key does the wrong thing or the cursor jumps: Confirm Vim is in Normal mode, then check whether a custom mapping, formatter, or plugin changes the key's behavior.

Navigating history and its limits

Vim history inspection
CommandWhat it doesCondition to remember
:undolistLists leaves of the undo treeShows branch leaves, not a full step-by-step chronology
:undo {N}Moves to a numbered undo stateThe requested state must exist
:earlier 10m / :later 10mNavigates by recorded timeThe state you reach depends on history and timing
:earlier 1fMoves backward by a write boundaryThe destination is not always the latest write

The value {N} in :undo {N} is an undo-history sequence number. :earlier 1f reaches an earlier write if nothing has changed since the latest write.

Vim undo limitations

Undo-tree navigation moves the buffer to an earlier state. It does not automatically reverse one arbitrary old edit or a visually selected portion while keeping every unrelated later change. If the state you want is not in the history, native Vim undo cannot retrieve it.

NoScript Team