summaryrefslogtreecommitdiff
path: root/runtime/doc/undo.txt
blob: 0659948fe7a89f0fe7d8a78622f54c65ad9cee84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
*undo.txt*      For Vim version 7.2.  Last change: 2009 Apr 12


		  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. Remarks about undo		|undo-remarks|

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

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

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

:u[ndo] {N}		Jump to after change number {N}.  See |undo-branches|
			for the meaning of {N}.  {not in Vi}

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

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

							*U*
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.

==============================================================================
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.
			{not in Vi}

This is most useful when you need to prompt the user halfway 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 an "u" command will undo the delete command and the previous
change.

To do the opposite, break a change into two undo blocks, 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|

==============================================================================
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   time ~
				4      10	 10:34:11
				18     4	 11:01:46

			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 "time" column is the time this change was made.

							*g-*
g-			Go to older text state.  With a count repeat that many
			times.  {not in Vi}
							*:ea* *:earlier*
:earlier {count}	Go to older text state {count} times.
:earlier {N}s		Go to older text state about {N} seconds before.
:earlier {N}m		Go to older text state about {N} minutes before.
:earlier {N}h		Go to older text state about {N} hours before.

							*g+*
g+			Go to newer text state.  With a count repeat that many
			times.  {not in Vi}
							*:lat* *:later*
:later {count}		Go to newer text state {count} times.
:later {N}s		Go to newer text state about {N} seconds later.
:later {N}m		Go to newer text state about {N} minutes later.
:later {N}h		Go to newer text state about {N} hours later.


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. Remarks about undo					*undo-remarks*

The number of changes that are remembered is set with the 'undolevels' option.
If it is zero, the Vi-compatible way is always used.  If it is negative no
undo is possible.  Use this if you are running out of memory.

Marks for the buffer ('a to 'z) are also saved and restored, together with the
text.  {Vi does this a little bit different}

When all changes have been undone, the buffer is not considered to be changed.
It is then possible to exit Vim with ":q" instead of ":q!" {not in Vi}.  Note
that this is relative to the last write of the file.  Typing "u" after ":w"
actually changes the buffer, compared to what was written, so the buffer is
considered changed then.

When manual |folding| is being used, the folds are not saved and restored.
Only changes completely within a fold will keep the fold as it was, because
the first and last line of the fold don't change.

The numbered registers can also be used for undoing deletes.  Each time you
delete text, it is put into register "1.  The contents of register "1 are
shifted to "2, etc.  The contents of register "9 are lost.  You can now get
back the most recent deleted text with the put command: '"1P'.  (also, if the
deleted text was the result of the last delete or copy operation, 'P' or 'p'
also works as this puts the contents of the unnamed register).  You can get
back the text of three deletes ago with '"3P'.

						*redo-register*
If you want to get back more than one part of deleted text, you can use a
special feature of the repeat command ".".  It will increase the number of the
register used.  So if you first do ""1P", the following "." will result in a
'"2P'.  Repeating this will result in all numbered registers being inserted.

Example:	If you deleted text with 'dd....' it can be restored with
		'"1P....'.

If you don't know in which register the deleted text is, you can use the
:display command.  An alternative is to try the first register with '"1P', and
if it is not what you want do 'u.'.  This will remove the contents of the
first put, and repeat the put command for the second register.  Repeat the
'u.' until you got what you want.

 vim:tw=78:ts=8:ft=help:norl: