How to Undo in Vim

How to Undo in Vim

by | Sep 19, 2024 | Useful Tech

Struggling to undo your changes in Vim? Trying to undo undoing to redo what you did before you undid it? Looking for a way to jump back in time to an earlier version or branch of your file? Find out here how to undo in Vim.

First, from Jamaica by Chance

The shaddock grew by chance, it seems, from two Jamaican citrus trees in, perhaps, the 17th century. The tree’s fruit was later also known as a pomelo and now, from its taste or how it grows in clusters, as a grapefruit.

Now, speaking of trees, branches, changes and fruits—this time those of your typing labor: Vim has trees to explore!

How to Undo in Vim

Time needed: 1 minute

To undo an action in Vim or Neovim:

  1. Switch to normal mode.

    Here’s how: Typically, press Esc to exit insert mode into normal mode in Vim.

  2. Press U.

    What this does (or undoes): Press U undoes one action, for example text input (entering insert mode, typing and exiting insert mode) or pasting text in Vim.
    Command mode: You can also use :undo in command mode.
    More than one action: Prepend the undo-command with a number to undo more than one change at a time; for example, 3 U or :3undo will undo 3 actions.
    Graphic Vim: In Vim with a graphical interface and toolbar, you can also click the Undo button, of course.

Looking for something to do you can undo? How to Change Case in Vim (to Upper, Lower, etc.)

Jump Back in Time with Vim Undo Branches

When you undo actions, and then make changes from that starting point, what you had originally done and then undone is not lost. Like a versioning system, Vim keeps track of changes in branches. You can restore to any of these branches and undo later changes (as well as having undone the original action).

See Vim Restore Points

To view points to which you can restore the state of a file in Vim:

  1. Press : In normal mode for Vim command mode.
  2. Type undolist.
    Type less: You can also abbreviate the command to undol.
  3. Press Enter.
    Seeing restore points to undo to in Vim

Vim will show a table that lets you identify changes and points to which you can restore the file.

numberidentifies the restore point
changescounts the changes to the text
whena date and time, relative if recent
savedthe restore point was written to the disk

Jump to a Restore Point

To jump back to a specific state with Vim undo branches:

  1. Enter command mode with :.
  2. Type u followed by the number of the restore point.
    Restore points: See above for identifying restore points in Vim
    Example: The command can look like :u 12 to jump to right after change number 12.
    Normal mode jumping: You can also press G - in normal mode to jump back one change from the undo list.
  3. Press Enter.

To go back in time in Vim:

  1. Type : for command mode.
  2. Enter earlier followed by relative time.
    What is time: You can give time as s (seconds), m (minutes), h (hours) and d (days) ago.
    Example: :earlier 5h will jump back 5 hours.
  3. Press Enter.

Redo in Vim

o undo undoing and re-do in Vim:

  • Press Ctrl R.
    Next restore point: You can also press G + to jump to the next restore point chronologically.

You can also use restore points to jump (back) forward in time:

  1. Press : for command mode.
  2. Type later followed time relative to the current state in the undo list.
    Undo list: See above for identifying restore points in Vim
    Example: :later 3m will jump ahead 3 minutes
  3. Press Enter.

How to Undo in Vim: FAQ

Can I undo in Vim insert mode while typing?

Yes, but undoing might not accomplish exactly what you’d expect it to do.

The command will still undo one action, that is most typically undoing all input since you entered insert mode.

To undo from insert mode in Vim:

  1. Press Ctrl O in insert mode.
  2. Now press U.

Can Vim undo changes after I have saved (or reopened) a file?

Yes.

Vim can keep a record of actions for undoing in a file separate from the edited file across sessions.

To enable this kind of persistent undo information in Vim:

  • Add set undofile to your .vimrc file.

By default, Vim keeps these files in the same directory as the original file under the same file name with .un~ appended.

You can collect all undo files in a hidden directory instead. To do so:

  • Add set undodir=~/.vim/undodir (or any other directory, of course) to. your .vimrc file.

(How to undo tested with Vim 9.0–9.1 and Neovim 0.9.5–0.10.1; first published October 2023, last updated September 2024)

Home » Useful Tech » How to Undo in Vim