Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 36 ยท Editing and Text Objects

Vim Undo, Redo and the Dot Command

Use u, Ctrl-r, U and the undo tree with g- and g+, understand undo steps, and repeat any change with the dot command for fast, repeatable editing.

Two features turn Vim from a keyboard editor into a fast one: a rock-solid undo system and the dot command, which repeats your last change. Together they let you try something, back out, and replay an edit across a file with a single key.

Undo and redo basics

Key Effect
u undo the last change
Ctrl-r redo
U undo all changes on the last changed line
:undo 0 undo everything back to the original text
:earlier 10m go back ten minutes in time
:later 5m go forward five minutes
:earlier 1f go back to the last file write

A "change" is a unit: one dd, one ciw plus the text you typed, or one whole Insert session. Moving the cursor with arrow keys during Insert mode splits the session into separate changes; using Esc and motions makes the units clearer.

Vim keeps the number of undo levels at 1000 by default and can store history on disk with the undofile option, so undo survives closing the file (chapter 6).

The undo tree

Most editors keep a straight line of history: if you undo, then make a new edit, the "future" you undid is lost. Vim keeps a tree. When you undo three times and type something new, a new branch forms and the old branch stays reachable.

  • g- moves back through the text states in time order, across branches.
  • g+ moves forward in time.
  • :undolist lists the leaves of the tree.
  • Plugins such as undotree draw the tree so you can pick a branch.

You can safely experiment: no edit is ever really lost unless you close without an undo file.

JS Undo tree model (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In a plain linear history the text one two three would be gone once you typed one other. In the tree it is still node 2, reachable with g-.

The dot command

Press . (period) and Vim repeats the last change: the operator, the motion or object, and anything typed in Insert mode. It does not repeat motions or yanks, only changes.

The idea that makes . powerful: design each edit so it is one repeatable change. Compare two ways to change foo to bar in many places.

  1. Position on the first foo with /foo<Enter>.
  2. ciw bar Esc, one change.
  3. n jumps to the next match; . repeats the change. Do n. as often as needed.

Two keys per occurrence, and you decide at every match, so you can skip with n. This "search, change, then n." rhythm beats a global substitute when you only want some of the matches.

The dot formula

A good dot-repeatable edit has three parts: a move to the target, a change that is one command, and a repeat with .. If your last command was A;<Esc> (append a semicolon), then j. adds one on the next line, and j. again. Press j. fast enough and it feels like applying the change to a list.

What breaks the dot

  • Using arrow keys or Ctrl-o inside Insert mode splits the change.
  • Making a motion between the change and your repeat is fine, but a second change replaces what . repeats.
  • Counts replay: 3. repeats the last change with count three.

When you do not want to disturb the dot register, use the same trick as in registers: pick the commands that do not count as changes (navigation, yy), or use a macro (next chapter).

Dot with visual and operators

Operators repeat too: after >ip, a . indents the paragraph under the cursor again. After gUiw, . uppercases another word. In Visual mode . repeats the operation on the same amount of text from the new cursor position, for example the same number of characters or lines. This is how vjj> then . can indent three lines at a time.

JS Dot repeat model (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Undo granularity tips

  • A single Insert session is one undo step. Long sessions are annoying to undo, so leave Insert at natural pauses.
  • Ctrl-g u in Insert mode starts a new undo step on purpose.
  • If you made many changes and want the original, :e! reloads the file from disk (discarding changes), while :undo 0 leaves you in the undo tree.
  • :earlier 1f is a safe "go back to what I last saved" that remains undoable.

Persisting undo to disk is a one-line setting that is worth having. It stores history in a folder per file, so tomorrow you can still press u on a file you closed today.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the dot command repeat?

    The dot repeats the last change, including its operator, object and typed text.
  2. 2. What is special about Vim's undo history?

    Undoing and then editing creates a new branch; g- and g+ can reach old branches.
  3. 3. What does :earlier 1f do?

    :earlier with an f suffix goes back by file-write states.
  4. 4. Which sequence appends a semicolon to the current and the next two lines using dot?

    A appends, Esc finishes the change, and j. moves and repeats the same change.

Summary

  • u undoes, Ctrl-r redoes, U restores a line, and :earlier and :later move through time.
  • The undo history is a tree; g- and g+ walk it chronologically.
  • . repeats the last change; design edits as one repeatable command.
  • A search plus n. changes some matches selectively.
  • Enable undofile so history survives restarts (chapter 6).

Next: Case, Join and Increment: Small Transformations