Quick links: help overview · quick reference · user manual toc · reference manual toc · faq
Go to keyword (shortcut: k)
Site search (shortcut: s)
undo.txt  	For Vim version 9.1.  Last change: 2025 Nov 09


		  VIM REFERENCE MANUAL	  by Bram Moolenaar


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. Undo blocks			undo-blocks
4. Undo branches		undo-branches
5. Undo persistence		undo-persistence
6. Remarks about undo		undo-remarks

==============================================================================
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}.

							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 before this that you must join with.

This doesn't work by itself, because the next key press will start a new
change again.  But you can do something like this: 

	:undojoin | delete

After this a "u" command will undo the delete command and the previous
change.
					undo-break undo-close-block
To do the opposite, use a new undo block for the next change, in Insert mode
use CTRL-G u.  This is useful if you want an insert command to be undoable in
parts.  E.g., for each sentence.  i_CTRL-G_u

Setting the value of 'undolevels' also closes the undo block.  Even when the
new value is equal to the old value.  Use g:undolevels to explicitly read
and write only the global value of 'undolevels'.  In Vim9 script: 
	&g:undolevels = &g:undolevels
In legacy script: 
	let &g:undolevels = &g:undolevels

Note that the similar-looking assignment `let &undolevels=&undolevels` does not
preserve the global option value of 'undolevels' in the event that the local
option has been set to a different value.  For example: 
	" Start with different global and local values for 'undolevels'.
	let &g:undolevels = 1000
	let &l:undolevels = 2000
	" This assignment changes the global option to 2000:
	let &undolevels = &undolevels

==============================================================================
4. Undo branches				undo-branches undo-tree

Above we only discussed one line of undo/redo.  But it is also possible to
branch off.  This happens when you undo a few changes and then make a new
change.  The undone changes become a branch.  You can go to that branch with
the following commands.

This is explained in the user manual: usr_32.txt.

							:undol :undolist
:undol[ist]		List the leafs in the tree of changes.  Example:
			   number changes  when               saved 
			       88      88  2010/01/04 14:25:53
			      108     107  08/07 12:47:51
			      136      46  13:33:01             7
			      166     164  3 seconds ago

			The "number" column is the change number.  This number
			continuously increases and can be used to identify a
			specific undo-able change, see :undo.
			The "changes" column is the number of changes to this
			leaf from the root of the tree.
			The "when" column is the date and time when this
			change was made.  The four possible formats are:
			    N seconds ago
			    HH:MM:SS             hour, minute, seconds
			    MM/DD HH:MM:SS       idem, with month and day
			    YYYY/MM/DD HH:MM:SS  idem, with year
			The "saved" column specifies, if this change was
			written to disk and which file write it was.  This can
			be used with the :later and :earlier commands.
			For more details use the undotree() function.

							g-
g-			Go to older text state.  With a count repeat that many
			times.
							:ea :earlier
:ea[rlier] {count}	Go to older text state {count} times.
:ea[rlier] {N}s		Go to older text state about {N} seconds before.
:ea[rlier] {N}m		Go to older text state about {N} minutes before.
:ea[rlier] {N}h		Go to older text state about {N} hours before.
:ea[rlier] {N}d		Go to older text state about {N} days before.

:ea[rlier] {N}f		Go to older text state {N} file writes before.
			When changes were made since the last write
			":earlier 1f" will revert the text to the state when
			it was written.  Otherwise it will go to the write
			before that.
			When at the state of the first file write, or when
			the file was not written, ":earlier 1f" will go to
			before the first change.

							g+
g+			Go to newer text state.  With a count repeat that many
			times.
							:lat :later
:lat[er] {count}	Go to newer text state {count} times.
:lat[er] {N}s		Go to newer text state about {N} seconds later.
:lat[er] {N}m		Go to newer text state about {N} minutes later.
:lat[er] {N}h		Go to newer text state about {N} hours later.
:lat[er] {N}d		Go to newer text state about {N} days later.

:lat[er] {N}f		Go to newer text state {N} file writes later.
			When at the state of the last file write, ":later 1f"
			will go to the newest text state.


Note that text states will become unreachable when undo information is cleared
for 'undolevels'.

Don't be surprised when moving through time shows multiple changes to take
place at a time.  This happens when moving through the undo tree and then
making a new change.

EXAMPLE

Start with this text:
	one two three 

Delete the first word by pressing "x" three times:
	ne two three 
	e two three 
	 two three 

Now undo that by pressing "u" three times:
	e two three 
	ne two three 
	one two three 

Delete the second word by pressing "x" three times:
	one wo three 
	one o three 
	one  three 

Now undo that by using "g-" three times:
	one o three 
	one wo three 
	 two three 

You are now back in the first undo branch, after deleting "one".  Repeating
"g-" will now bring you back to the original text:
	e two three 
	ne two three 
	one two three 

Jump to the last change with ":later 1h":
	one  three 

And back to the start again with ":earlier 1h":
	one two three 


Note that using "u" and CTRL-R will not get you to all possible text states
while repeating "g-" and "g+" does.

==============================================================================
5. Undo persistence		undo-persistence persistent-undo

When unloading a buffer Vim normally destroys the tree of undos created for
that buffer.  By setting the 'undofile' option, Vim will automatically save
your undo