blob: 34d805433344f8fd22082f6965edce481365266b [file] [log] [blame]
Bram Moolenaareb490412022-06-28 13:44:46 +01001*undo.txt* For Vim version 9.0. Last change: 2022 Jun 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*
Bram Moolenaara6c27c42019-05-09 19:16:22 +020022u Undo [count] changes.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
24 *:u* *:un* *:undo*
Bram Moolenaara6c27c42019-05-09 19:16:22 +020025:u[ndo] Undo one change.
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|
Bram Moolenaar25c9c682019-05-05 18:13:34 +020028 for the meaning of {N}.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 *CTRL-R*
Bram Moolenaara6c27c42019-05-09 19:16:22 +020031CTRL-R Redo [count] changes which were undone.
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
33 *:red* *:redo* *redo*
Bram Moolenaara6c27c42019-05-09 19:16:22 +020034:red[o] Redo one change which was undone.
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
36 *U*
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020037U Undo all latest changes on one line, the line where
38 the latest change was made. |U| itself also counts as
39 a change, and thus |U| undoes a previous |U|.
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
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'.
Bram Moolenaar7cba6c02013-09-05 22:13:31 +020058There is the Vim way ('u' excluded) and the Vi-compatible way ('u' included).
Bram Moolenaar071d4272004-06-13 20:20:40 +000059In 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:
Bram Moolenaar1b884a02020-12-10 21:11:27 +010068The undo command undoes the previous change, and also the previous undo
69command. The redo command repeats the previous undo command. It does NOT
70repeat a change command, use "." for that.
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
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
Bram Moolenaar822ff862014-06-12 21:46:14 +020097This is most useful when you need to prompt the user halfway through a change.
98For example in a function that calls |getchar()|. Do make sure that there was
99a related change before this that you must join with.
Bram Moolenaare224ffa2006-03-01 00:01:28 +0000100
101This doesn't work by itself, because the next key press will start a new
102change again. But you can do something like this: >
103
104 :undojoin | delete
105
Bram Moolenaar1b884a02020-12-10 21:11:27 +0100106After this a "u" command will undo the delete command and the previous
Bram Moolenaare224ffa2006-03-01 00:01:28 +0000107change.
Bram Moolenaarcfa8f9a2022-06-03 21:59:47 +0100108 *undo-break*
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100109To do the opposite, break a change into two undo blocks, in Insert mode use
110CTRL-G u. This is useful if you want an insert command to be undoable in
111parts. E.g., for each sentence. |i_CTRL-G_u|
Bram Moolenaarcfa8f9a2022-06-03 21:59:47 +0100112
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200113Setting the value of 'undolevels' also breaks undo. Even when the new value
Bram Moolenaarcfa8f9a2022-06-03 21:59:47 +0100114is equal to the old value. In |Vim9| script: >
115 &undolevels = &undolevels
116In legacy script: >
117 let &undolevels = &undolevels
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100118
Bram Moolenaare224ffa2006-03-01 00:01:28 +0000119==============================================================================
Bram Moolenaar18144c82006-04-12 21:52:12 +00001204. Undo branches *undo-branches* *undo-tree*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000121
Bram Moolenaar76916e62006-03-21 21:23:25 +0000122Above we only discussed one line of undo/redo. But it is also possible to
123branch off. This happens when you undo a few changes and then make a new
124change. The undone changes become a branch. You can go to that branch with
125the following commands.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000126
Bram Moolenaarc01140a2006-03-24 22:21:52 +0000127This is explained in the user manual: |usr_32.txt|.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000128
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000129 *:undol* *:undolist*
130:undol[ist] List the leafs in the tree of changes. Example:
Bram Moolenaar2a8a3ec2011-01-08 16:06:37 +0100131 number changes when saved ~
132 88 88 2010/01/04 14:25:53
133 108 107 08/07 12:47:51
134 136 46 13:33:01 7
135 166 164 3 seconds ago
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000136
137 The "number" column is the change number. This number
138 continuously increases and can be used to identify a
139 specific undo-able change, see |:undo|.
140 The "changes" column is the number of changes to this
141 leaf from the root of the tree.
Bram Moolenaar2a8a3ec2011-01-08 16:06:37 +0100142 The "when" column is the date and time when this
143 change was made. The four possible formats are:
144 N seconds ago
145 HH:MM:SS hour, minute, seconds
146 MM/DD HH:MM:SS idem, with month and day
147 YYYY/MM/DD HH:MM:SS idem, with year
Bram Moolenaar24ea3ba2010-09-19 19:01:21 +0200148 The "saved" column specifies, if this change was
149 written to disk and which file write it was. This can
Bram Moolenaar4a748032010-09-30 21:47:56 +0200150 be used with the |:later| and |:earlier| commands.
Bram Moolenaara800b422010-06-27 01:15:55 +0200151 For more details use the |undotree()| function.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000152
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000153 *g-*
154g- Go to older text state. With a count repeat that many
Bram Moolenaar25c9c682019-05-05 18:13:34 +0200155 times.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000156 *:ea* *:earlier*
157:earlier {count} Go to older text state {count} times.
158:earlier {N}s Go to older text state about {N} seconds before.
159:earlier {N}m Go to older text state about {N} minutes before.
160:earlier {N}h Go to older text state about {N} hours before.
Bram Moolenaar730cde92010-06-27 05:18:54 +0200161:earlier {N}d Go to older text state about {N} days before.
162
163:earlier {N}f Go to older text state {N} file writes before.
Bram Moolenaar24ea3ba2010-09-19 19:01:21 +0200164 When changes were made since the last write
Bram Moolenaar730cde92010-06-27 05:18:54 +0200165 ":earlier 1f" will revert the text to the state when
166 it was written. Otherwise it will go to the write
167 before that.
168 When at the state of the first file write, or when
169 the file was not written, ":earlier 1f" will go to
170 before the first change.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000171
172 *g+*
173g+ Go to newer text state. With a count repeat that many
Bram Moolenaar25c9c682019-05-05 18:13:34 +0200174 times.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000175 *:lat* *:later*
176:later {count} Go to newer text state {count} times.
177:later {N}s Go to newer text state about {N} seconds later.
178:later {N}m Go to newer text state about {N} minutes later.
179:later {N}h Go to newer text state about {N} hours later.
Bram Moolenaar730cde92010-06-27 05:18:54 +0200180:later {N}d Go to newer text state about {N} days later.
181
182:later {N}f Go to newer text state {N} file writes later.
183 When at the state of the last file write, ":later 1f"
184 will go to the newest text state.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000185
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000186
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000187Note that text states will become unreachable when undo information is cleared
188for 'undolevels'.
189
190Don't be surprised when moving through time shows multiple changes to take
191place at a time. This happens when moving through the undo tree and then
192making a new change.
193
194EXAMPLE
195
196Start with this text:
197 one two three ~
198
199Delete the first word by pressing "x" three times:
200 ne two three ~
201 e two three ~
202 two three ~
203
204Now undo that by pressing "u" three times:
205 e two three ~
206 ne two three ~
207 one two three ~
208
209Delete the second word by pressing "x" three times:
210 one wo three ~
211 one o three ~
212 one three ~
213
214Now undo that by using "g-" three times:
215 one o three ~
216 one wo three ~
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000217 two three ~
218
219You are now back in the first undo branch, after deleting "one". Repeating
220"g-" will now bring you back to the original text:
221 e two three ~
222 ne two three ~
223 one two three ~
224
225Jump to the last change with ":later 1h":
226 one three ~
227
228And back to the start again with ":earlier 1h":
229 one two three ~
230
231
232Note that using "u" and CTRL-R will not get you to all possible text states
233while repeating "g-" and "g+" does.
234
235==============================================================================
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002365. Undo persistence *undo-persistence* *persistent-undo*
237
238When unloading a buffer Vim normally destroys the tree of undos created for
239that buffer. By setting the 'undofile' option, Vim will automatically save
240your undo history when you write a file and restore undo history when you edit
241the file again.
242
243The 'undofile' option is checked after writing a file, before the BufWritePost
244autocommands. If you want to control what files to write undo information
245for, you can use a BufWritePre autocommand: >
246 au BufWritePre /tmp/* setlocal noundofile
247
248Vim saves undo trees in a separate undo file, one for each edited file, using
249a simple scheme that maps filesystem paths directly to undo files. Vim will
250detect if an undo file is no longer synchronized with the file it was written
251for (with a hash of the file contents) and ignore it when the file was changed
Bram Moolenaar05365702010-10-27 18:34:44 +0200252after the undo file was written, to prevent corruption. An undo file is also
Bram Moolenaarbaca7f72013-09-22 14:42:24 +0200253ignored if its owner differs from the owner of the edited file, except when
254the owner of the undo file is the current user. Set 'verbose' to get a
255message about that when opening a file.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200256
257Undo files are normally saved in the same directory as the file. This can be
258changed with the 'undodir' option.
259
Bram Moolenaard592deb2022-06-17 15:42:40 +0100260When the file is encrypted, the text in the undo file is also encrypted. The
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200261same key and method is used. |encryption|
262
Bram Moolenaard09091d2019-01-17 16:07:22 +0100263Note that text properties are not stored in the undo file. You can restore
264text properties so long as a buffer is loaded, but you cannot restore them
265from an undo file. Rationale: It would require the associated text property
266types to be defined in exactly the same was as before, which cannot be
267guaranteed.
268
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200269You can also save and restore undo histories by using ":wundo" and ":rundo"
270respectively:
271 *:wundo* *:rundo*
272:wundo[!] {file}
273 Write undo history to {file}.
274 When {file} exists and it does not look like an undo file
275 (the magic number at the start of the file is wrong), then
276 this fails, unless the ! was added.
277 If it exists and does look like an undo file it is
Bram Moolenaar664f3cf2019-12-07 16:03:51 +0100278 overwritten. If there is no undo-history, nothing will be
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100279 written.
280 Implementation detail: Overwriting happens by first deleting
281 the existing file and then creating a new file with the same
282 name. So it is not possible to overwrite an existing undofile
283 in a write-protected directory.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200284
285:rundo {file} Read undo history from {file}.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200286
287You can use these in autocommands to explicitly specify the name of the
288history file. E.g.: >
289
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200290 au BufReadPost * call ReadUndo()
291 au BufWritePost * call WriteUndo()
292 func ReadUndo()
Bram Moolenaarc51cf032022-02-26 12:25:45 +0000293 if filereadable(expand('%:h') .. '/UNDO/' .. expand('%:t'))
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200294 rundo %:h/UNDO/%:t
295 endif
296 endfunc
297 func WriteUndo()
Bram Moolenaarc51cf032022-02-26 12:25:45 +0000298 let dirname = expand('%:h') .. '/UNDO'
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200299 if !isdirectory(dirname)
300 call mkdir(dirname)
301 endif
302 wundo %:h/UNDO/%:t
303 endfunc
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200304
305You should keep 'undofile' off, otherwise you end up with two undo files for
306every write.
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200307
308You can use the |undofile()| function to find out the file name that Vim would
309use.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200310
311Note that while reading/writing files and 'undofile' is set most errors will
312be silent, unless 'verbose' is set. With :wundo and :rundo you will get more
313error messages, e.g., when the file cannot be read or written.
314
315NOTE: undo files are never deleted by Vim. You need to delete them yourself.
316
317Reading an existing undo file may fail for several reasons:
318*E822* It cannot be opened, because the file permissions don't allow it.
319*E823* The magic number at the start of the file doesn't match. This usually
320 means it is not an undo file.
321*E824* The version number of the undo file indicates that it's written by a
322 newer version of Vim. You need that newer version to open it. Don't
323 write the buffer if you want to keep the undo info in the file.
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200324"File contents changed, cannot use undo info"
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200325 The file text differs from when the undo file was written. This means
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200326 the undo file cannot be used, it would corrupt the text. This also
327 happens when 'encoding' differs from when the undo file was written.
Bram Moolenaar9db58062010-05-29 20:33:07 +0200328*E825* The undo file does not contain valid contents and cannot be used.
Bram Moolenaar56be9502010-06-06 14:20:26 +0200329*E826* The undo file is encrypted but decryption failed.
330*E827* The undo file is encrypted but this version of Vim does not support
331 encryption. Open the file with another Vim.
332*E832* The undo file is encrypted but 'key' is not set, the text file is not
333 encrypted. This would happen if the text file was written by Vim
334 encrypted at first, and later overwritten by not encrypted text.
335 You probably want to delete this undo file.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200336"Not reading undo file, owner differs"
337 The undo file is owned by someone else than the owner of the text
338 file. For safety the undo file is not used.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200339
340Writing an undo file may fail for these reasons:
341*E828* The file to be written cannot be created. Perhaps you do not have
342 write permissions in the directory.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200343"Cannot write undo file in any directory in 'undodir'"
344 None of the directories in 'undodir' can be used.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200345"Will not overwrite with undo file, cannot read"
346 A file exists with the name of the undo file to be written, but it
347 cannot be read. You may want to delete this file or rename it.
348"Will not overwrite, this is not an undo file"
349 A file exists with the name of the undo file to be written, but it
350 does not start with the right magic number. You may want to delete
351 this file or rename it.
Bram Moolenaar24ea3ba2010-09-19 19:01:21 +0200352"Skipping undo file write, nothing to undo"
353 There is no undo information to be written, nothing has been changed
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200354 or 'undolevels' is negative.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200355*E829* An error occurred while writing the undo file. You may want to try
356 again.
357
358==============================================================================
3596. Remarks about undo *undo-remarks*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
361The number of changes that are remembered is set with the 'undolevels' option.
362If it is zero, the Vi-compatible way is always used. If it is negative no
363undo is possible. Use this if you are running out of memory.
364
Bram Moolenaar945e2db2010-06-05 17:43:32 +0200365 *clear-undo*
366When you set 'undolevels' to -1 the undo information is not immediately
367cleared, this happens at the next change. To force clearing the undo
368information you can use these commands: >
369 :let old_undolevels = &undolevels
370 :set undolevels=-1
371 :exe "normal a \<BS>\<Esc>"
372 :let &undolevels = old_undolevels
373 :unlet old_undolevels
374
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375Marks for the buffer ('a to 'z) are also saved and restored, together with the
Bram Moolenaar25c9c682019-05-05 18:13:34 +0200376text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377
378When all changes have been undone, the buffer is not considered to be changed.
Bram Moolenaar25c9c682019-05-05 18:13:34 +0200379It is then possible to exit Vim with ":q" instead of ":q!". Note
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380that this is relative to the last write of the file. Typing "u" after ":w"
381actually changes the buffer, compared to what was written, so the buffer is
382considered changed then.
383
384When manual |folding| is being used, the folds are not saved and restored.
385Only changes completely within a fold will keep the fold as it was, because
386the first and last line of the fold don't change.
387
388The numbered registers can also be used for undoing deletes. Each time you
389delete text, it is put into register "1. The contents of register "1 are
390shifted to "2, etc. The contents of register "9 are lost. You can now get
391back the most recent deleted text with the put command: '"1P'. (also, if the
392deleted text was the result of the last delete or copy operation, 'P' or 'p'
393also works as this puts the contents of the unnamed register). You can get
394back the text of three deletes ago with '"3P'.
395
396 *redo-register*
397If you want to get back more than one part of deleted text, you can use a
398special feature of the repeat command ".". It will increase the number of the
Bram Moolenaarcb80aa22020-10-26 21:12:46 +0100399register used. So if you first do '"1P', the following "." will result in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400'"2P'. Repeating this will result in all numbered registers being inserted.
401
402Example: If you deleted text with 'dd....' it can be restored with
403 '"1P....'.
404
405If you don't know in which register the deleted text is, you can use the
406:display command. An alternative is to try the first register with '"1P', and
407if it is not what you want do 'u.'. This will remove the contents of the
408first put, and repeat the put command for the second register. Repeat the
409'u.' until you got what you want.
410
Bram Moolenaard473c8c2018-08-11 18:00:22 +0200411 vim:tw=78:ts=8:noet:ft=help:norl: