Vim 101: Undo

You'll notice experienced Vim users switch back into Normal mode as often as is practical. One reason for this is due to the way Vim tracks changes in a buffer. A change is a command entered in the Normal or Command-line modes, or edits made while Insert mode is active. Pressing u or typing :undo will revert the change. This command accepts an argument, so typing 3u will undo three changes. Typing CTRL-R or :redo will make the change again, and a [count] argument can be supplied to CTRL-R as well.

Branches

Undo branches (:help undo-branches) provide another way of managing sets of changes. Typing :undol will display them. Why is this useful? Well, imagine what happens when you undo something and then start editing again. In a linear undo system, this would effectively orphan the changes that occurred after edit resumed. With Vim's branch-based system, it's possible to get back to any given state more easily.

After making a number of changes and typing :undol, you'll see a list that looks like this:

:undol

The number column is the number of the change: these are incremented as the buffer is edited. It can be supplied as an argument to :undo, so typing :u 4 will go back to change number 4. The changes column shows the number of changes from the root of the tree; this number can decrease and increase between each change set. The when column is the date and time that the change was made -- short times will be displayed in seconds, then various date formats will be used as the change ages.

The fact the time is tracked is useful because it means we can also jump to specific moments in time. For example, typing :earlier 15m will return the buffer's state to 15 minutes ago. Vim might be an incredible time machine, but these capabilities should not be misconstrued for practicing proper version control.

Undo Settings

Vim can save the current set of undo branches (:help undo-persistence). This can be configured by using the undofile and undodir settings:

set undodir=~/.vim/undodir
set undofile on

A related setting is undolevels which controls the maximum number of changes that can be undone.

blog comments powered by Disqus