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>uin Insert mode marks the current position as an undo boundary- The text typed before
<C-g>ubecomes one undo unit; text typed after becomes the next - Pressing
uin Normal mode undoes back to the most recent break point - Pressing
uagain 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>uto common punctuation to get automatic undo breaks:
After this, every sentence or clause is its own undo step — closer to how undo works in most text editorsinoremap . .<C-g>u inoremap ! !<C-g>u inoremap ? ?<C-g>u inoremap , ,<C-g>u - 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