blob: e9ed1b2cb536d3cbb0405066b424815bf8453fc0 [file] [log] [blame]
Bram Moolenaar5302d9e2011-09-14 17:55:08 +02001*undo.txt* For Vim version 7.3. Last change: 2011 Aug 02
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*
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020038U Undo all latest changes on one line, the line where
39 the latest change was made. |U| itself also counts as
40 a change, and thus |U| undoes a previous |U|.
41 {Vi: while not moved off of the last modified line}
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43The last changes are remembered. You can use the undo and redo commands above
44to revert the text to how it was before each change. You can also apply the
45changes again, getting back the text before the undo.
46
47The "U" command is treated by undo/redo just like any other command. Thus a
48"u" command undoes a "U" command and a 'CTRL-R' command redoes it again. When
49mixing "U", "u" and 'CTRL-R' you will notice that the "U" command will
50restore the situation of a line to before the previous "U" command. This may
51be confusing. Try it out to get used to it.
52The "U" command will always mark the buffer as changed. When "U" changes the
53buffer back to how it was without changes, it is still considered changed.
54Use "u" to undo changes until the buffer becomes unchanged.
55
56==============================================================================
572. Two ways of undo *undo-two-ways*
58
59How undo and redo commands work depends on the 'u' flag in 'cpoptions'.
60There is the Vim way ('u' excluded) and the vi-compatible way ('u' included).
61In the Vim way, "uu" undoes two changes. In the Vi-compatible way, "uu" does
62nothing (undoes an undo).
63
64'u' excluded, the Vim way:
65You can go back in time with the undo command. You can then go forward again
66with the redo command. If you make a new change after the undo command,
67the redo will not be possible anymore.
68
69'u' included, the Vi-compatible way:
70The undo command undoes the previous change, and also the previous undo command.
71The redo command repeats the previous undo command. It does NOT repeat a
72change command, use "." for that.
73
74Examples Vim way Vi-compatible way ~
75"uu" two times undo no-op
76"u CTRL-R" no-op two times undo
77
78Rationale: Nvi uses the "." command instead of CTRL-R. Unfortunately, this
79 is not Vi compatible. For example "dwdwu." in Vi deletes two
80 words, in Nvi it does nothing.
81
82==============================================================================
Bram Moolenaare224ffa2006-03-01 00:01:28 +0000833. Undo blocks *undo-blocks*
84
85One undo command normally undoes a typed command, no matter how many changes
86that command makes. This sequence of undo-able changes forms an undo block.
87Thus if the typed key(s) call a function, all the commands in the function are
88undone together.
89
90If you want to write a function or script that doesn't create a new undoable
91change but joins in with the previous change use this command:
92
Bram Moolenaar57657d82006-04-21 22:12:41 +000093 *:undoj* *:undojoin* *E790*
Bram Moolenaare224ffa2006-03-01 00:01:28 +000094:undoj[oin] Join further changes with the previous undo block.
95 Warning: Use with care, it may prevent the user from
Bram Moolenaar57657d82006-04-21 22:12:41 +000096 properly undoing changes. Don't use this after undo
97 or redo.
Bram Moolenaare224ffa2006-03-01 00:01:28 +000098 {not in Vi}
99
100This is most useful when you need to prompt the user halfway a change. For
101example in a function that calls |getchar()|. Do make sure that there was a
102related change before this that you must join with.
103
104This doesn't work by itself, because the next key press will start a new
105change again. But you can do something like this: >
106
107 :undojoin | delete
108
109After this an "u" command will undo the delete command and the previous
110change.
111
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100112To do the opposite, break a change into two undo blocks, in Insert mode use
113CTRL-G u. This is useful if you want an insert command to be undoable in
114parts. E.g., for each sentence. |i_CTRL-G_u|
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200115Setting the value of 'undolevels' also breaks undo. Even when the new value
116is equal to the old value.
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100117
Bram Moolenaare224ffa2006-03-01 00:01:28 +0000118==============================================================================
Bram Moolenaar18144c82006-04-12 21:52:12 +00001194. Undo branches *undo-branches* *undo-tree*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000120
Bram Moolenaar76916e62006-03-21 21:23:25 +0000121Above we only discussed one line of undo/redo. But it is also possible to
122branch off. This happens when you undo a few changes and then make a new
123change. The undone changes become a branch. You can go to that branch with
124the following commands.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000125
Bram Moolenaarc01140a2006-03-24 22:21:52 +0000126This is explained in the user manual: |usr_32.txt|.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000127
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000128 *:undol* *:undolist*
129:undol[ist] List the leafs in the tree of changes. Example:
Bram Moolenaar2a8a3ec2011-01-08 16:06:37 +0100130 number changes when saved ~
131 88 88 2010/01/04 14:25:53
132 108 107 08/07 12:47:51
133 136 46 13:33:01 7
134 166 164 3 seconds ago
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000135
136 The "number" column is the change number. This number
137 continuously increases and can be used to identify a
138 specific undo-able change, see |:undo|.
139 The "changes" column is the number of changes to this
140 leaf from the root of the tree.
Bram Moolenaar2a8a3ec2011-01-08 16:06:37 +0100141 The "when" column is the date and time when this
142 change was made. The four possible formats are:
143 N seconds ago
144 HH:MM:SS hour, minute, seconds
145 MM/DD HH:MM:SS idem, with month and day
146 YYYY/MM/DD HH:MM:SS idem, with year
Bram Moolenaar24ea3ba2010-09-19 19:01:21 +0200147 The "saved" column specifies, if this change was
148 written to disk and which file write it was. This can
Bram Moolenaar4a748032010-09-30 21:47:56 +0200149 be used with the |:later| and |:earlier| commands.
Bram Moolenaara800b422010-06-27 01:15:55 +0200150 For more details use the |undotree()| function.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000151
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000152 *g-*
153g- Go to older text state. With a count repeat that many
154 times. {not in Vi}
155 *:ea* *:earlier*
156:earlier {count} Go to older text state {count} times.
157:earlier {N}s Go to older text state about {N} seconds before.
158:earlier {N}m Go to older text state about {N} minutes before.
159:earlier {N}h Go to older text state about {N} hours before.
Bram Moolenaar730cde92010-06-27 05:18:54 +0200160:earlier {N}d Go to older text state about {N} days before.
161
162:earlier {N}f Go to older text state {N} file writes before.
Bram Moolenaar24ea3ba2010-09-19 19:01:21 +0200163 When changes were made since the last write
Bram Moolenaar730cde92010-06-27 05:18:54 +0200164 ":earlier 1f" will revert the text to the state when
165 it was written. Otherwise it will go to the write
166 before that.
167 When at the state of the first file write, or when
168 the file was not written, ":earlier 1f" will go to
169 before the first change.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000170
171 *g+*
172g+ Go to newer text state. With a count repeat that many
173 times. {not in Vi}
174 *:lat* *:later*
175:later {count} Go to newer text state {count} times.
176:later {N}s Go to newer text state about {N} seconds later.
177:later {N}m Go to newer text state about {N} minutes later.
178:later {N}h Go to newer text state about {N} hours later.
Bram Moolenaar730cde92010-06-27 05:18:54 +0200179:later {N}d Go to newer text state about {N} days later.
180
181:later {N}f Go to newer text state {N} file writes later.
182 When at the state of the last file write, ":later 1f"
183 will go to the newest text state.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000184
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000185
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000186Note that text states will become unreachable when undo information is cleared
187for 'undolevels'.
188
189Don't be surprised when moving through time shows multiple changes to take
190place at a time. This happens when moving through the undo tree and then
191making a new change.
192
193EXAMPLE
194
195Start with this text:
196 one two three ~
197
198Delete the first word by pressing "x" three times:
199 ne two three ~
200 e two three ~
201 two three ~
202
203Now undo that by pressing "u" three times:
204 e two three ~
205 ne two three ~
206 one two three ~
207
208Delete the second word by pressing "x" three times:
209 one wo three ~
210 one o three ~
211 one three ~
212
213Now undo that by using "g-" three times:
214 one o three ~
215 one wo three ~
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000216 two three ~
217
218You are now back in the first undo branch, after deleting "one". Repeating
219"g-" will now bring you back to the original text:
220 e two three ~
221 ne two three ~
222 one two three ~
223
224Jump to the last change with ":later 1h":
225 one three ~
226
227And back to the start again with ":earlier 1h":
228 one two three ~
229
230
231Note that using "u" and CTRL-R will not get you to all possible text states
232while repeating "g-" and "g+" does.
233
234==============================================================================
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002355. Undo persistence *undo-persistence* *persistent-undo*
236
237When unloading a buffer Vim normally destroys the tree of undos created for
238that buffer. By setting the 'undofile' option, Vim will automatically save
239your undo history when you write a file and restore undo history when you edit
240the file again.
241
242The 'undofile' option is checked after writing a file, before the BufWritePost
243autocommands. If you want to control what files to write undo information
244for, you can use a BufWritePre autocommand: >
245 au BufWritePre /tmp/* setlocal noundofile
246
247Vim saves undo trees in a separate undo file, one for each edited file, using
248a simple scheme that maps filesystem paths directly to undo files. Vim will
249detect if an undo file is no longer synchronized with the file it was written
250for (with a hash of the file contents) and ignore it when the file was changed
Bram Moolenaar05365702010-10-27 18:34:44 +0200251after the undo file was written, to prevent corruption. An undo file is also
252ignored if its owner differs from the owner of the edited file. Set 'verbose'
253to get a message about that.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200254
255Undo files are normally saved in the same directory as the file. This can be
256changed with the 'undodir' option.
257
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200258When the file is encrypted, the text in the undo file is also crypted. The
259same key and method is used. |encryption|
260
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200261You can also save and restore undo histories by using ":wundo" and ":rundo"
262respectively:
263 *:wundo* *:rundo*
264:wundo[!] {file}
265 Write undo history to {file}.
266 When {file} exists and it does not look like an undo file
267 (the magic number at the start of the file is wrong), then
268 this fails, unless the ! was added.
269 If it exists and does look like an undo file it is
270 overwritten.
271 {not in Vi}
272
273:rundo {file} Read undo history from {file}.
274 {not in Vi}
275
276You can use these in autocommands to explicitly specify the name of the
277history file. E.g.: >
278
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200279 au BufReadPost * call ReadUndo()
280 au BufWritePost * call WriteUndo()
281 func ReadUndo()
282 if filereadable(expand('%:h'). '/UNDO/' . expand('%:t'))
283 rundo %:h/UNDO/%:t
284 endif
285 endfunc
286 func WriteUndo()
287 let dirname = expand('%:h') . '/UNDO'
288 if !isdirectory(dirname)
289 call mkdir(dirname)
290 endif
291 wundo %:h/UNDO/%:t
292 endfunc
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200293
294You should keep 'undofile' off, otherwise you end up with two undo files for
295every write.
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200296
297You can use the |undofile()| function to find out the file name that Vim would
298use.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200299
300Note that while reading/writing files and 'undofile' is set most errors will
301be silent, unless 'verbose' is set. With :wundo and :rundo you will get more
302error messages, e.g., when the file cannot be read or written.
303
304NOTE: undo files are never deleted by Vim. You need to delete them yourself.
305
306Reading an existing undo file may fail for several reasons:
307*E822* It cannot be opened, because the file permissions don't allow it.
308*E823* The magic number at the start of the file doesn't match. This usually
309 means it is not an undo file.
310*E824* The version number of the undo file indicates that it's written by a
311 newer version of Vim. You need that newer version to open it. Don't
312 write the buffer if you want to keep the undo info in the file.
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200313"File contents changed, cannot use undo info"
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200314 The file text differs from when the undo file was written. This means
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200315 the undo file cannot be used, it would corrupt the text. This also
316 happens when 'encoding' differs from when the undo file was written.
Bram Moolenaar9db58062010-05-29 20:33:07 +0200317*E825* The undo file does not contain valid contents and cannot be used.
Bram Moolenaar56be9502010-06-06 14:20:26 +0200318*E826* The undo file is encrypted but decryption failed.
319*E827* The undo file is encrypted but this version of Vim does not support
320 encryption. Open the file with another Vim.
321*E832* The undo file is encrypted but 'key' is not set, the text file is not
322 encrypted. This would happen if the text file was written by Vim
323 encrypted at first, and later overwritten by not encrypted text.
324 You probably want to delete this undo file.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200325"Not reading undo file, owner differs"
326 The undo file is owned by someone else than the owner of the text
327 file. For safety the undo file is not used.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200328
329Writing an undo file may fail for these reasons:
330*E828* The file to be written cannot be created. Perhaps you do not have
331 write permissions in the directory.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200332"Cannot write undo file in any directory in 'undodir'"
333 None of the directories in 'undodir' can be used.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200334"Will not overwrite with undo file, cannot read"
335 A file exists with the name of the undo file to be written, but it
336 cannot be read. You may want to delete this file or rename it.
337"Will not overwrite, this is not an undo file"
338 A file exists with the name of the undo file to be written, but it
339 does not start with the right magic number. You may want to delete
340 this file or rename it.
Bram Moolenaar24ea3ba2010-09-19 19:01:21 +0200341"Skipping undo file write, nothing to undo"
342 There is no undo information to be written, nothing has been changed
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200343 or 'undolevels' is negative.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200344*E829* An error occurred while writing the undo file. You may want to try
345 again.
346
347==============================================================================
3486. Remarks about undo *undo-remarks*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349
350The number of changes that are remembered is set with the 'undolevels' option.
351If it is zero, the Vi-compatible way is always used. If it is negative no
352undo is possible. Use this if you are running out of memory.
353
Bram Moolenaar945e2db2010-06-05 17:43:32 +0200354 *clear-undo*
355When you set 'undolevels' to -1 the undo information is not immediately
356cleared, this happens at the next change. To force clearing the undo
357information you can use these commands: >
358 :let old_undolevels = &undolevels
359 :set undolevels=-1
360 :exe "normal a \<BS>\<Esc>"
361 :let &undolevels = old_undolevels
362 :unlet old_undolevels
363
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364Marks for the buffer ('a to 'z) are also saved and restored, together with the
365text. {Vi does this a little bit different}
366
367When all changes have been undone, the buffer is not considered to be changed.
368It is then possible to exit Vim with ":q" instead of ":q!" {not in Vi}. Note
369that this is relative to the last write of the file. Typing "u" after ":w"
370actually changes the buffer, compared to what was written, so the buffer is
371considered changed then.
372
373When manual |folding| is being used, the folds are not saved and restored.
374Only changes completely within a fold will keep the fold as it was, because
375the first and last line of the fold don't change.
376
377The numbered registers can also be used for undoing deletes. Each time you
378delete text, it is put into register "1. The contents of register "1 are
379shifted to "2, etc. The contents of register "9 are lost. You can now get
380back the most recent deleted text with the put command: '"1P'. (also, if the
381deleted text was the result of the last delete or copy operation, 'P' or 'p'
382also works as this puts the contents of the unnamed register). You can get
383back the text of three deletes ago with '"3P'.
384
385 *redo-register*
386If you want to get back more than one part of deleted text, you can use a
387special feature of the repeat command ".". It will increase the number of the
388register used. So if you first do ""1P", the following "." will result in a
389'"2P'. Repeating this will result in all numbered registers being inserted.
390
391Example: If you deleted text with 'dd....' it can be restored with
392 '"1P....'.
393
394If you don't know in which register the deleted text is, you can use the
395:display command. An alternative is to try the first register with '"1P', and
396if it is not what you want do 'u.'. This will remove the contents of the
397first put, and repeat the put command for the second register. Repeat the
398'u.' until you got what you want.
399
400 vim:tw=78:ts=8:ft=help:norl: