Saturday, August 29, 2009

Vim undo redo

SkyHi @ Saturday, August 29, 2009

Undo and redo *undo-redo*

The basics are explained in section |02.5| of the user manual.

1. Undo and redo commands |undo-commands|
2. Two ways of undo |undo-two-ways|
3. Remarks about undo |undo-remarks|


1. Undo and redo commands *undo-commands*

or *undo* ** *u*
u Undo [count] changes. {Vi: only one level}

*:u* *:un* *:undo*
:u[ndo] Undo one change. {Vi: only one level}

CTRL-R Redo [count] changes which were undone. {Vi: redraw

*:red* *:redo* *redo*
:red[o] Redo one change which was undone. {Vi: no redo}

U Undo all latest changes on one line. {Vi: while not
moved off of it}

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.

The "U" command is treated by undo/redo just like any other command. Thus a
"u" command undoes a "U" command and a 'CTRL-R' command redoes it again. When
mixing "U", "u" and 'CTRL-R' you will notice that the "U" command will
restore the situation of a line to before the previous "U" command. This may
be confusing. Try it out to get used to it.
The "U" command will always mark the buffer as changed. When "U" changes the
buffer back to how it was without changes, it is still considered changed.
Use "u" to undo changes until the buffer becomes unchanged.


2. Two ways of undo *undo-two-ways*

How undo and redo commands work depends on the 'u' flag in 'cpoptions'.
There is the Vim way ('u' excluded) and the vi-compatible way ('u' included).
In the Vim way, "uu" undoes two changes. In the Vi-compatible way, "uu" does
nothing (undoes an undo).

'u' excluded, the Vim way:
You can go back in time with the undo command. You can then go forward again
with the redo command. If you make a new change after the undo command,
the redo will not be possible anymore.

'u' included, the Vi-compatible way:
The undo command undoes the previous change, and also the previous undo command.
The redo command repeats the previous undo command. It does NOT repeat a
change command, use "." for that.

Examples Vim way Vi-compatible way
"uu" two times undo no-op
"u CTRL-R" no-op two times undo

Rationale: Nvi uses the "." command instead of CTRL-R. Unfortunately, this
is not Vi compatible. For example "dwdwu." in Vi deletes two
words, in Nvi it does nothing.