Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 19 down vote accepted

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.

share|improve this answer
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. – fruglemonkey Feb 4 at 0:06

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:

share|improve this answer
    
"Chunky undo" - best description I've seen. – Matt Parker Feb 12 at 23:42

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]

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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