vimtricks.wiki Concise Vim tricks, one at a time.

How do I create a custom undo checkpoint while in Insert mode so I can undo in smaller steps?

Answer

<C-g>u

Explanation

By default, Vim treats an entire Insert mode session (from entering Insert mode to pressing <Esc>) as a single undo unit. <C-g>u inserts an undo break point mid-session, splitting the edit into two separate undo steps. This gives you fine-grained undo granularity without ever leaving Insert mode.

How it works

  • <C-g>u in Insert mode marks the current position as an undo boundary
  • The text typed before <C-g>u becomes one undo unit; text typed after becomes the next
  • Pressing u in Normal mode undoes back to the most recent break point
  • Pressing u again undoes the preceding segment
  • This does not move the cursor or modify the text

Example

Typing a long sentence and adding break points at each clause:

[Insert mode]
First clause, <C-g>u  second clause, <C-g>u  third clause.

Now u undoes one clause at a time instead of the entire sentence.

Tips

  • Map <C-g>u to common punctuation to get automatic undo breaks:
    inoremap . .<C-g>u
    inoremap ! !<C-g>u
    inoremap ? ?<C-g>u
    inoremap , ,<C-g>u
    
    After this, every sentence or clause is its own undo step — closer to how undo works in most text editors
  • Also useful before a potentially destructive action inside Insert mode (like <C-u> to delete to start of line) so you can recover easily
  • The same break point also works for the repeat command (.): each segment between break points is independently repeatable

Next

How do I run a search and replace only within a visually selected region?