Skip to Content
Learning the vi and Vim Editors, 7th Edition
book

Learning the vi and Vim Editors, 7th Edition

by Arnold Robbins, Elbert Hannah, Linda Lamb
July 2008
Beginner
492 pages
16h
English
O'Reilly Media, Inc.
Content preview from Learning the vi and Vim Editors, 7th Edition

Undoing Undos

Beyond the convenience of undoing an arbitrary number of edits, Vim offers an interesting twist called branching undos.

To use this feature, first decide how much control you want over undoing edits. Use the undolevels option to define the number of undoable changes you can make in an edit session. The default is 1,000, which is probably more than enough for most users. If you want vi compatibility, set undolevels to zero:

:set undolevels=0

In vi, the undo command u is basically a toggle between the file’s current state and its most recent change. The first undo reverts to the state before the last change. The next undo redoes the undone change. Vim behaves quite differently, and therefore the commands are implemented differently.

Instead of toggling the most recent change, repeated invocations of Vim’s undo rolls back the state of the file through the most recent changes, in order, for as many changes as defined by the undolevels option. Because the undo command u only moves backward, we need a command to roll forward and “redo” changes. Vim does this with the redo command, :redo, or the CTRL-R key. The CTRL-R key accepts a numeric prefix to redo several changes at once.

When rolling forward and backward through changes with the redo (CTRL-R) and undo (u) commands, Vim maintains a map of the file’s state and knows when the last possible undo has been performed. When all possible undos are done, Vim resets the file’s modified status, which allows quitting without the

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning the vi and Vim Editors, 8th Edition

Learning the vi and Vim Editors, 8th Edition

Arnold Robbins, Elbert Hannah
Vim Masterclass

Vim Masterclass

Jason Cannon
Mastering Vim

Mastering Vim

Ruslan Osipov

Publisher Resources

ISBN: 9780596529833Errata Page