Home Explore Blog CI



neovim

1st chunk of `runtime/doc/undo.txt`
67362586f4de66f864368b3973e6d41b4098b001c87567190000000100000fa1
*undo.txt*      Nvim


		  VIM REFERENCE MANUAL    by Bram Moolenaar


Undo and redo						*undo-redo*

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

                                      Type |gO| to see the table of contents.

==============================================================================
1. Undo and redo commands				*undo-commands*

<Undo>		or					*undo* *<Undo>* *u*
u			Undo [count] changes.

							*:u* *:un* *:undo*
:u[ndo]			Undo one change.
								*E830*
:u[ndo] {N}		Jump to after change number {N}.  See |undo-branches|
			for the meaning of {N}.

:u[ndo]!		Undo one change and remove it from undo history.
								*E5767*
:u[ndo]! {N}		Like ":u[ndo] {N}", but forget all changes in the
			current undo branch up until {N}. You may only use
			":undo! {N}" to move backwards in the same undo
			branch, not to redo or switch to a different undo
			branch.

							*CTRL-R*
CTRL-R			Redo [count] changes which were undone.

							*:red* *:redo* *redo*
:red[o]			Redo one change which was undone.

							*U*
U			Undo all latest changes on one line, the line where
			the latest change was made. |U| itself also counts as
			a change, and thus |U| undoes a previous |U|.

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.

==============================================================================
3. Undo blocks						*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.

If you want to write a function or script that doesn't create a new undoable
change but joins in with the previous change use this command:

						*:undoj* *:undojoin* *E790*
:undoj[oin]		Join further changes with the previous undo block.
			Warning: Use with care, it may prevent the user from
			properly undoing changes.  Don't use this after undo
			or redo.

This is most useful when you need to prompt the user halfway through a change.
For example in a function that calls |getchar()|.  Do make sure that there was
a related change

Title: Undo and Redo Commands in Vim
Summary
This section of the Vim documentation explains how to use the undo and redo commands (u, :undo, CTRL-R, :redo, U) to revert and reapply changes in a buffer. It details two different undo/redo behaviors controlled by the 'u' flag in 'cpoptions': the Vim way and the Vi-compatible way. It also introduces undo blocks, which group multiple changes into a single undoable unit, and the ':undojoin' command, which allows joining further changes with the previous undo block.