6

The undo command feels a bit random. Sometimes it undoes massive chunks, sometimes tiny changes. How is the cut-off point determined?

2

1 Answer 1

6

It undo the last command (e.g. i, c, d).

The insert (i) command can indeed be either:

  • rather large if you type a long text without leaving the insert mode or
  • rather short if you just make a small change and leave the insert mode or use the right or left arrow to move the cursor.

In insert mode you you can type Ctrlgu to split the insert operation and create shorter undo chunk.

On the opposite you can type CtrlgU right before the right -> or left <- arrow to avoid to split the insert operation and make a longer undo chunk.

Somes add the following mapping in their .vimrc file to make sure every words inserted in insert mode are in their own undo chunk.

:inoremap <space> <C-g>u<space>

But the recommended Vim way is to leave insert mode after each "meaningful" operation.

1
  • 5
    I'm not personally a fan of the <space> thing; really, the idea is to leave insert mode when you pause (i.e., make hitting <esc> a habit). cf. :help undo-blocks, :help :changes, :help change.txt
    – D. Ben Knoble
    Commented Jun 22, 2022 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.