blob: 6e2308ff1b05c492580e014efe36e71a1b27df3f [file] [log] [blame]
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02001*undo.txt* For Vim version 7.3a. Last change: 2009 Apr 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7Undo and redo *undo-redo*
8
9The basics are explained in section |02.5| of the user manual.
10
111. Undo and redo commands |undo-commands|
122. Two ways of undo |undo-two-ways|
Bram Moolenaare224ffa2006-03-01 00:01:28 +0000133. Undo blocks |undo-blocks|
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000144. Undo branches |undo-branches|
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200155. Undo persistence |undo-persistence|
166. Remarks about undo |undo-remarks|
Bram Moolenaar071d4272004-06-13 20:20:40 +000017
18==============================================================================
191. Undo and redo commands *undo-commands*
20
21<Undo> or *undo* *<Undo>* *u*
22u Undo [count] changes. {Vi: only one level}
23
24 *:u* *:un* *:undo*
25:u[ndo] Undo one change. {Vi: only one level}
Bram Moolenaar55debbe2010-05-23 23:34:36 +020026 *E830*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +000027:u[ndo] {N} Jump to after change number {N}. See |undo-branches|
28 for the meaning of {N}. {not in Vi}
29
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 *CTRL-R*
31CTRL-R Redo [count] changes which were undone. {Vi: redraw
32 screen}
33
34 *:red* *:redo* *redo*
35:red[o] Redo one change which was undone. {Vi: no redo}
36
37 *U*
38U Undo all latest changes on one line. {Vi: while not
39 moved off of it}
40
41The last changes are remembered. You can use the undo and redo commands above
42to revert the text to how it was before each change. You can also apply the
43changes again, getting back the text before the undo.
44
45The "U" command is treated by undo/redo just like any other command. Thus a
46"u" command undoes a "U" command and a 'CTRL-R' command redoes it again. When
47mixing "U", "u" and 'CTRL-R' you will notice that the "U" command will
48restore the situation of a line to before the previous "U" command. This may
49be confusing. Try it out to get used to it.
50The "U" command will always mark the buffer as changed. When "U" changes the
51buffer back to how it was without changes, it is still considered changed.
52Use "u" to undo changes until the buffer becomes unchanged.
53
54==============================================================================
552. Two ways of undo *undo-two-ways*
56
57How undo and redo commands work depends on the 'u' flag in 'cpoptions'.
58There is the Vim way ('u' excluded) and the vi-compatible way ('u' included).
59In the Vim way, "uu" undoes two changes. In the Vi-compatible way, "uu" does
60nothing (undoes an undo).
61
62'u' excluded, the Vim way:
63You can go back in time with the undo command. You can then go forward again
64with the redo command. If you make a new change after the undo command,
65the redo will not be possible anymore.
66
67'u' included, the Vi-compatible way:
68The undo command undoes the previous change, and also the previous undo command.
69The redo command repeats the previous undo command. It does NOT repeat a
70change command, use "." for that.
71
72Examples Vim way Vi-compatible way ~
73"uu" two times undo no-op
74"u CTRL-R" no-op two times undo
75
76Rationale: Nvi uses the "." command instead of CTRL-R. Unfortunately, this
77 is not Vi compatible. For example "dwdwu." in Vi deletes two
78 words, in Nvi it does nothing.
79
80==============================================================================
Bram Moolenaare224ffa2006-03-01 00:01:28 +0000813. Undo blocks *undo-blocks*
82
83One undo command normally undoes a typed command, no matter how many changes
84that command makes. This sequence of undo-able changes forms an undo block.
85Thus if the typed key(s) call a function, all the commands in the function are
86undone together.
87
88If you want to write a function or script that doesn't create a new undoable
89change but joins in with the previous change use this command:
90
Bram Moolenaar57657d82006-04-21 22:12:41 +000091 *:undoj* *:undojoin* *E790*
Bram Moolenaare224ffa2006-03-01 00:01:28 +000092:undoj[oin] Join further changes with the previous undo block.
93 Warning: Use with care, it may prevent the user from
Bram Moolenaar57657d82006-04-21 22:12:41 +000094 properly undoing changes. Don't use this after undo
95 or redo.
Bram Moolenaare224ffa2006-03-01 00:01:28 +000096 {not in Vi}
97
98This is most useful when you need to prompt the user halfway a change. For
99example in a function that calls |getchar()|. Do make sure that there was a
100related change before this that you must join with.
101
102This doesn't work by itself, because the next key press will start a new
103change again. But you can do something like this: >
104
105 :undojoin | delete
106
107After this an "u" command will undo the delete command and the previous
108change.
109
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100110To do the opposite, break a change into two undo blocks, in Insert mode use
111CTRL-G u. This is useful if you want an insert command to be undoable in
112parts. E.g., for each sentence. |i_CTRL-G_u|
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200113Setting the value of 'undolevels' also breaks undo. Even when the new value
114is equal to the old value.
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100115
Bram Moolenaare224ffa2006-03-01 00:01:28 +0000116==============================================================================
Bram Moolenaar18144c82006-04-12 21:52:12 +00001174. Undo branches *undo-branches* *undo-tree*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000118
Bram Moolenaar76916e62006-03-21 21:23:25 +0000119Above we only discussed one line of undo/redo. But it is also possible to
120branch off. This happens when you undo a few changes and then make a new
121change. The undone changes become a branch. You can go to that branch with
122the following commands.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000123
Bram Moolenaarc01140a2006-03-24 22:21:52 +0000124This is explained in the user manual: |usr_32.txt|.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000125
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000126 *:undol* *:undolist*
127:undol[ist] List the leafs in the tree of changes. Example:
128 number changes time ~
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000129 4 10 10:34:11
130 18 4 11:01:46
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000131
132 The "number" column is the change number. This number
133 continuously increases and can be used to identify a
134 specific undo-able change, see |:undo|.
135 The "changes" column is the number of changes to this
136 leaf from the root of the tree.
137 The "time" column is the time this change was made.
138
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000139 *g-*
140g- Go to older text state. With a count repeat that many
141 times. {not in Vi}
142 *:ea* *:earlier*
143:earlier {count} Go to older text state {count} times.
144:earlier {N}s Go to older text state about {N} seconds before.
145:earlier {N}m Go to older text state about {N} minutes before.
146:earlier {N}h Go to older text state about {N} hours before.
147
148 *g+*
149g+ Go to newer text state. With a count repeat that many
150 times. {not in Vi}
151 *:lat* *:later*
152:later {count} Go to newer text state {count} times.
153:later {N}s Go to newer text state about {N} seconds later.
154:later {N}m Go to newer text state about {N} minutes later.
155:later {N}h Go to newer text state about {N} hours later.
156
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000157
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000158Note that text states will become unreachable when undo information is cleared
159for 'undolevels'.
160
161Don't be surprised when moving through time shows multiple changes to take
162place at a time. This happens when moving through the undo tree and then
163making a new change.
164
165EXAMPLE
166
167Start with this text:
168 one two three ~
169
170Delete the first word by pressing "x" three times:
171 ne two three ~
172 e two three ~
173 two three ~
174
175Now undo that by pressing "u" three times:
176 e two three ~
177 ne two three ~
178 one two three ~
179
180Delete the second word by pressing "x" three times:
181 one wo three ~
182 one o three ~
183 one three ~
184
185Now undo that by using "g-" three times:
186 one o three ~
187 one wo three ~
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000188 two three ~
189
190You are now back in the first undo branch, after deleting "one". Repeating
191"g-" will now bring you back to the original text:
192 e two three ~
193 ne two three ~
194 one two three ~
195
196Jump to the last change with ":later 1h":
197 one three ~
198
199And back to the start again with ":earlier 1h":
200 one two three ~
201
202
203Note that using "u" and CTRL-R will not get you to all possible text states
204while repeating "g-" and "g+" does.
205
206==============================================================================
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002075. Undo persistence *undo-persistence* *persistent-undo*
208
209When unloading a buffer Vim normally destroys the tree of undos created for
210that buffer. By setting the 'undofile' option, Vim will automatically save
211your undo history when you write a file and restore undo history when you edit
212the file again.
213
214The 'undofile' option is checked after writing a file, before the BufWritePost
215autocommands. If you want to control what files to write undo information
216for, you can use a BufWritePre autocommand: >
217 au BufWritePre /tmp/* setlocal noundofile
218
219Vim saves undo trees in a separate undo file, one for each edited file, using
220a simple scheme that maps filesystem paths directly to undo files. Vim will
221detect if an undo file is no longer synchronized with the file it was written
222for (with a hash of the file contents) and ignore it when the file was changed
223after the undo file was written, to prevent corruption.
224
225Undo files are normally saved in the same directory as the file. This can be
226changed with the 'undodir' option.
227
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200228When the file is encrypted, the text in the undo file is also crypted. The
229same key and method is used. |encryption|
230
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200231You can also save and restore undo histories by using ":wundo" and ":rundo"
232respectively:
233 *:wundo* *:rundo*
234:wundo[!] {file}
235 Write undo history to {file}.
236 When {file} exists and it does not look like an undo file
237 (the magic number at the start of the file is wrong), then
238 this fails, unless the ! was added.
239 If it exists and does look like an undo file it is
240 overwritten.
241 {not in Vi}
242
243:rundo {file} Read undo history from {file}.
244 {not in Vi}
245
246You can use these in autocommands to explicitly specify the name of the
247history file. E.g.: >
248
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200249 au BufReadPost * call ReadUndo()
250 au BufWritePost * call WriteUndo()
251 func ReadUndo()
252 if filereadable(expand('%:h'). '/UNDO/' . expand('%:t'))
253 rundo %:h/UNDO/%:t
254 endif
255 endfunc
256 func WriteUndo()
257 let dirname = expand('%:h') . '/UNDO'
258 if !isdirectory(dirname)
259 call mkdir(dirname)
260 endif
261 wundo %:h/UNDO/%:t
262 endfunc
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200263
264You should keep 'undofile' off, otherwise you end up with two undo files for
265every write.
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200266
267You can use the |undofile()| function to find out the file name that Vim would
268use.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200269
270Note that while reading/writing files and 'undofile' is set most errors will
271be silent, unless 'verbose' is set. With :wundo and :rundo you will get more
272error messages, e.g., when the file cannot be read or written.
273
274NOTE: undo files are never deleted by Vim. You need to delete them yourself.
275
276Reading an existing undo file may fail for several reasons:
277*E822* It cannot be opened, because the file permissions don't allow it.
278*E823* The magic number at the start of the file doesn't match. This usually
279 means it is not an undo file.
280*E824* The version number of the undo file indicates that it's written by a
281 newer version of Vim. You need that newer version to open it. Don't
282 write the buffer if you want to keep the undo info in the file.
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200283"File contents changed, cannot use undo info"
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200284 The file text differs from when the undo file was written. This means
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200285 the undo file cannot be used, it would corrupt the text. This also
286 happens when 'encoding' differs from when the undo file was written.
Bram Moolenaar9db58062010-05-29 20:33:07 +0200287*E825* The undo file does not contain valid contents and cannot be used.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200288"Not reading undo file, owner differs"
289 The undo file is owned by someone else than the owner of the text
290 file. For safety the undo file is not used.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200291
292Writing an undo file may fail for these reasons:
293*E828* The file to be written cannot be created. Perhaps you do not have
294 write permissions in the directory.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200295"Cannot write undo file in any directory in 'undodir'"
296 None of the directories in 'undodir' can be used.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200297"Will not overwrite with undo file, cannot read"
298 A file exists with the name of the undo file to be written, but it
299 cannot be read. You may want to delete this file or rename it.
300"Will not overwrite, this is not an undo file"
301 A file exists with the name of the undo file to be written, but it
302 does not start with the right magic number. You may want to delete
303 this file or rename it.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200304"Skipping undo file write, noting to undo"
305 There is no undo information not be written, nothing has been changed
306 or 'undolevels' is negative.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200307*E829* An error occurred while writing the undo file. You may want to try
308 again.
309
310==============================================================================
3116. Remarks about undo *undo-remarks*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312
313The number of changes that are remembered is set with the 'undolevels' option.
314If it is zero, the Vi-compatible way is always used. If it is negative no
315undo is possible. Use this if you are running out of memory.
316
Bram Moolenaar945e2db2010-06-05 17:43:32 +0200317 *clear-undo*
318When you set 'undolevels' to -1 the undo information is not immediately
319cleared, this happens at the next change. To force clearing the undo
320information you can use these commands: >
321 :let old_undolevels = &undolevels
322 :set undolevels=-1
323 :exe "normal a \<BS>\<Esc>"
324 :let &undolevels = old_undolevels
325 :unlet old_undolevels
326
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327Marks for the buffer ('a to 'z) are also saved and restored, together with the
328text. {Vi does this a little bit different}
329
330When all changes have been undone, the buffer is not considered to be changed.
331It is then possible to exit Vim with ":q" instead of ":q!" {not in Vi}. Note
332that this is relative to the last write of the file. Typing "u" after ":w"
333actually changes the buffer, compared to what was written, so the buffer is
334considered changed then.
335
336When manual |folding| is being used, the folds are not saved and restored.
337Only changes completely within a fold will keep the fold as it was, because
338the first and last line of the fold don't change.
339
340The numbered registers can also be used for undoing deletes. Each time you
341delete text, it is put into register "1. The contents of register "1 are
342shifted to "2, etc. The contents of register "9 are lost. You can now get
343back the most recent deleted text with the put command: '"1P'. (also, if the
344deleted text was the result of the last delete or copy operation, 'P' or 'p'
345also works as this puts the contents of the unnamed register). You can get
346back the text of three deletes ago with '"3P'.
347
348 *redo-register*
349If you want to get back more than one part of deleted text, you can use a
350special feature of the repeat command ".". It will increase the number of the
351register used. So if you first do ""1P", the following "." will result in a
352'"2P'. Repeating this will result in all numbered registers being inserted.
353
354Example: If you deleted text with 'dd....' it can be restored with
355 '"1P....'.
356
357If you don't know in which register the deleted text is, you can use the
358:display command. An alternative is to try the first register with '"1P', and
359if it is not what you want do 'u.'. This will remove the contents of the
360first put, and repeat the put command for the second register. Repeat the
361'u.' until you got what you want.
362
363 vim:tw=78:ts=8:ft=help:norl: