18

When using u or CTL-R to undo or re-do an edit in vim, I seem to alter chunks of text, not just the most recent keystroke.

What determines the size of the chunk that is considered to be a single edit?

3 Answers 3

20

You are looking for the definition of undo-blocks.

From :h undo-blocks:

One undo command normally undoes a typed command, no matter how many changes that command makes. This sequence of undo-able changes forms an undo block. Thus if the typed key(s) call a function, all the commands in the function are undone together.

The same block is used for redo. From :h redo:

The last changes are remembered. You can use the undo and redo commands above to revert the text to how it was before each change. You can also apply the changes again, getting back the text before the undo.

1
  • 4
    You can also manually break apart a single undo-block with the command CTRL-G u in insert mode, which is useful if you have some insert mode macros. Feb 4, 2016 at 0:06
8

Vim's modal editing is often viewed as an operator (e.g. c, d, ...) applied over a motion (e.g. iw, w, }, ...). As a wonderful by product Vim gets chunky undo's. Where as other editors have to do some guessing to make many small undo's into undo blocks, Vim does this naturally. Another side affect is this gives you a repeat operator, . (aka the dot command).

Your question specifically mention keystrokes so I assume you want to know how to undo small changes while in insert mode. The answer is this is not "the Vim way". "The Vim Way" means you will be in insert mode for short burst at a time. This means when mistakes are made in insert mode you often just exit to normal mode and correct the text or just do an undo, u, and rewrite the text.

Sometimes there is need to split an undo block while in insert mode. This can be accomplished via <c-g>u while in insert mode. See :h :undoj and :h undo-blocks for more information.

For more help I suggest the following:

:h undo-blocks
:h :undoj
:h undo-tree
:h persistent-undo
:h undo.txt

There are also some screencasts on this subject:

1
  • "Chunky undo" - best description I've seen. Feb 12, 2016 at 23:42
6

You can think of it this way too: every time you go to insert mode to edit text and you hit Esc, that will form a block. If you try to undo that block, you will go to the same position before you entered insert mode.

Therefore you can "save" your changes just by exiting insert mode from time to time: let's say you go to insert mode and write a function. You could continue coding and go to the next function however sometimes it's preferable to go back to normal mode and switch to insert mode again. In that way, you can have your code structured in a simple way and if you need to go back you can know in what position you will end up.

Note: if you use arrows for moving, it's like if you pressed Esc +[hjkl]

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.