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

How do I move the cursor in insert mode without creating an undo break point?

Answer

<C-g>U{motion}

Explanation

By default, moving the cursor with arrow keys while in insert mode creates an undo break — meaning a subsequent u will undo only back to when you last moved, not the full insertion session. <C-g>U followed by a cursor motion suppresses this undo-break behaviour, keeping the entire edit as a single undoable unit.

This is most useful when creating custom insert-mode mappings that move the cursor (for example, to skip over closing brackets or quotes) without fragmenting the undo history.

How it works

  • <C-g> — enters a special insert-mode sub-mode for miscellaneous commands
  • U — signals that the next cursor movement should NOT create an undo break
  • {motion} — any cursor movement key: <Left>, <Right>, <Up>, <Down>, <Home>, <End>

Example

Add this to your vimrc to make the left and right arrow keys work in insert mode without breaking undo:

inoremap <Left>  <C-g>U<Left>
inoremap <Right> <C-g>U<Right>

Now typing hello world, pressing <Left> three times, then inserting Vim , and pressing u will undo the entire sequence in one step instead of splitting it at each arrow keypress.

Tips

  • This is the mechanism behind automatic bracket completion plugins that position the cursor between delimiters without breaking undo.
  • Compare with <C-g>u (lowercase): that command deliberately creates an undo breakpoint, while <C-g>U prevents one.
  • Only affects undo granularity, not the text that is typed or deleted.

Next

How do I add a per-window title bar showing the current file name in Neovim?