1*undo.txt* For Vim version 8.2. Last change: 2020 Nov 30 2 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| 133. Undo blocks |undo-blocks| 144. Undo branches |undo-branches| 155. Undo persistence |undo-persistence| 166. Remarks about undo |undo-remarks| 17 18============================================================================== 191. Undo and redo commands *undo-commands* 20 21<Undo> or *undo* *<Undo>* *u* 22u Undo [count] changes. 23 24 *:u* *:un* *:undo* 25:u[ndo] Undo one change. 26 *E830* 27:u[ndo] {N} Jump to after change number {N}. See |undo-branches| 28 for the meaning of {N}. 29 30 *CTRL-R* 31CTRL-R Redo [count] changes which were undone. 32 33 *:red* *:redo* *redo* 34:red[o] Redo one change which was undone. 35 36 *U* 37U 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|. 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 69command. The redo command repeats the previous undo command. It does NOT 70repeat a change 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============================================================================== 813. 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 91 *:undoj* *:undojoin* *E790* 92:undoj[oin] Join further changes with the previous undo block. 93 Warning: Use with care, it may prevent the user from 94 properly undoing changes. Don't use this after undo 95 or redo. 96 97This 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. 100 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 106After this a "u" command will undo the delete command and the previous 107change. 108 109To 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| 112Setting the value of 'undolevels' also breaks undo. Even when the new value 113is equal to the old value. 114 115============================================================================== 1164. Undo branches *undo-branches* *undo-tree* 117 118Above we only discussed one line of undo/redo. But it is also possible to 119branch off. This happens when you undo a few changes and then make a new 120change. The undone changes become a branch. You can go to that branch with 121the following commands. 122 123This is explained in the user manual: |usr_32.txt|. 124 125 *:undol* *:undolist* 126:undol[ist] List the leafs in the tree of changes. Example: 127 number changes when saved ~ 128 88 88 2010/01/04 14:25:53 129 108 107 08/07 12:47:51 130 136 46 13:33:01 7 131 166 164 3 seconds ago 132 133 The "number" column is the change number. This number 134 continuously increases and can be used to identify a 135 specific undo-able change, see |:undo|. 136 The "changes" column is the number of changes to this 137 leaf from the root of the tree. 138 The "when" column is the date and time when this 139 change was made. The four possible formats are: 140 N seconds ago 141 HH:MM:SS hour, minute, seconds 142 MM/DD HH:MM:SS idem, with month and day 143 YYYY/MM/DD HH:MM:SS idem, with year 144 The "saved" column specifies, if this change was 145 written to disk and which file write it was. This can 146 be used with the |:later| and |:earlier| commands. 147 For more details use the |undotree()| function. 148 149 *g-* 150g- Go to older text state. With a count repeat that many 151 times. 152 *:ea* *:earlier* 153:earlier {count} Go to older text state {count} times. 154:earlier {N}s Go to older text state about {N} seconds before. 155:earlier {N}m Go to older text state about {N} minutes before. 156:earlier {N}h Go to older text state about {N} hours before. 157:earlier {N}d Go to older text state about {N} days before. 158 159:earlier {N}f Go to older text state {N} file writes before. 160 When changes were made since the last write 161 ":earlier 1f" will revert the text to the state when 162 it was written. Otherwise it will go to the write 163 before that. 164 When at the state of the first file write, or when 165 the file was not written, ":earlier 1f" will go to 166 before the first change. 167 168 *g+* 169g+ Go to newer text state. With a count repeat that many 170 times. 171 *:lat* *:later* 172:later {count} Go to newer text state {count} times. 173:later {N}s Go to newer text state about {N} seconds later. 174:later {N}m Go to newer text state about {N} minutes later. 175:later {N}h Go to newer text state about {N} hours later. 176:later {N}d Go to newer text state about {N} days later. 177 178:later {N}f Go to newer text state {N} file writes later. 179 When at the state of the last file write, ":later 1f" 180 will go to the newest text state. 181 182 183Note that text states will become unreachable when undo information is cleared 184for 'undolevels'. 185 186Don't be surprised when moving through time shows multiple changes to take 187place at a time. This happens when moving through the undo tree and then 188making a new change. 189 190EXAMPLE 191 192Start with this text: 193 one two three ~ 194 195Delete the first word by pressing "x" three times: 196 ne two three ~ 197 e two three ~ 198 two three ~ 199 200Now undo that by pressing "u" three times: 201 e two three ~ 202 ne two three ~ 203 one two three ~ 204 205Delete the second word by pressing "x" three times: 206 one wo three ~ 207 one o three ~ 208 one three ~ 209 210Now undo that by using "g-" three times: 211 one o three ~ 212 one wo three ~ 213 two three ~ 214 215You are now back in the first undo branch, after deleting "one". Repeating 216"g-" will now bring you back to the original text: 217 e two three ~ 218 ne two three ~ 219 one two three ~ 220 221Jump to the last change with ":later 1h": 222 one three ~ 223 224And back to the start again with ":earlier 1h": 225 one two three ~ 226 227 228Note that using "u" and CTRL-R will not get you to all possible text states 229while repeating "g-" and "g+" does. 230 231============================================================================== 2325. Undo persistence *undo-persistence* *persistent-undo* 233 234When unloading a buffer Vim normally destroys the tree of undos created for 235that buffer. By setting the 'undofile' option, Vim will automatically save 236your undo history when you write a file and restore undo history when you edit 237the file again. 238 239The 'undofile' option is checked after writing a file, before the BufWritePost 240autocommands. If you want to control what files to write undo information 241for, you can use a BufWritePre autocommand: > 242 au BufWritePre /tmp/* setlocal noundofile 243 244Vim saves undo trees in a separate undo file, one for each edited file, using 245a simple scheme that maps filesystem paths directly to undo files. Vim will 246detect if an undo file is no longer synchronized with the file it was written 247for (with a hash of the file contents) and ignore it when the file was changed 248after the undo file was written, to prevent corruption. An undo file is also 249ignored if its owner differs from the owner of the edited file, except when 250the owner of the undo file is the current user. Set 'verbose' to get a 251message about that when opening a file. 252 253Undo files are normally saved in the same directory as the file. This can be 254changed with the 'undodir' option. 255 256When the file is encrypted, the text in the undo file is also crypted. The 257same key and method is used. |encryption| 258 259Note that text properties are not stored in the undo file. You can restore 260text properties so long as a buffer is loaded, but you cannot restore them 261from an undo file. Rationale: It would require the associated text property 262types to be defined in exactly the same was as before, which cannot be 263guaranteed. 264 265You can also save and restore undo histories by using ":wundo" and ":rundo" 266respectively: 267 *:wundo* *:rundo* 268:wundo[!] {file} 269 Write undo history to {file}. 270 When {file} exists and it does not look like an undo file 271 (the magic number at the start of the file is wrong), then 272 this fails, unless the ! was added. 273 If it exists and does look like an undo file it is 274 overwritten. If there is no undo-history, nothing will be 275 written. 276 Implementation detail: Overwriting happens by first deleting 277 the existing file and then creating a new file with the same 278 name. So it is not possible to overwrite an existing undofile 279 in a write-protected directory. 280 281:rundo {file} Read undo history from {file}. 282 283You can use these in autocommands to explicitly specify the name of the 284history file. E.g.: > 285 286 au BufReadPost * call ReadUndo() 287 au BufWritePost * call WriteUndo() 288 func ReadUndo() 289 if filereadable(expand('%:h'). '/UNDO/' . expand('%:t')) 290 rundo %:h/UNDO/%:t 291 endif 292 endfunc 293 func WriteUndo() 294 let dirname = expand('%:h') . '/UNDO' 295 if !isdirectory(dirname) 296 call mkdir(dirname) 297 endif 298 wundo %:h/UNDO/%:t 299 endfunc 300 301You should keep 'undofile' off, otherwise you end up with two undo files for 302every write. 303 304You can use the |undofile()| function to find out the file name that Vim would 305use. 306 307Note that while reading/writing files and 'undofile' is set most errors will 308be silent, unless 'verbose' is set. With :wundo and :rundo you will get more 309error messages, e.g., when the file cannot be read or written. 310 311NOTE: undo files are never deleted by Vim. You need to delete them yourself. 312 313Reading an existing undo file may fail for several reasons: 314*E822* It cannot be opened, because the file permissions don't allow it. 315*E823* The magic number at the start of the file doesn't match. This usually 316 means it is not an undo file. 317*E824* The version number of the undo file indicates that it's written by a 318 newer version of Vim. You need that newer version to open it. Don't 319 write the buffer if you want to keep the undo info in the file. 320"File contents changed, cannot use undo info" 321 The file text differs from when the undo file was written. This means 322 the undo file cannot be used, it would corrupt the text. This also 323 happens when 'encoding' differs from when the undo file was written. 324*E825* The undo file does not contain valid contents and cannot be used. 325*E826* The undo file is encrypted but decryption failed. 326*E827* The undo file is encrypted but this version of Vim does not support 327 encryption. Open the file with another Vim. 328*E832* The undo file is encrypted but 'key' is not set, the text file is not 329 encrypted. This would happen if the text file was written by Vim 330 encrypted at first, and later overwritten by not encrypted text. 331 You probably want to delete this undo file. 332"Not reading undo file, owner differs" 333 The undo file is owned by someone else than the owner of the text 334 file. For safety the undo file is not used. 335 336Writing an undo file may fail for these reasons: 337*E828* The file to be written cannot be created. Perhaps you do not have 338 write permissions in the directory. 339"Cannot write undo file in any directory in 'undodir'" 340 None of the directories in 'undodir' can be used. 341"Will not overwrite with undo file, cannot read" 342 A file exists with the name of the undo file to be written, but it 343 cannot be read. You may want to delete this file or rename it. 344"Will not overwrite, this is not an undo file" 345 A file exists with the name of the undo file to be written, but it 346 does not start with the right magic number. You may want to delete 347 this file or rename it. 348"Skipping undo file write, nothing to undo" 349 There is no undo information to be written, nothing has been changed 350 or 'undolevels' is negative. 351*E829* An error occurred while writing the undo file. You may want to try 352 again. 353 354============================================================================== 3556. Remarks about undo *undo-remarks* 356 357The number of changes that are remembered is set with the 'undolevels' option. 358If it is zero, the Vi-compatible way is always used. If it is negative no 359undo is possible. Use this if you are running out of memory. 360 361 *clear-undo* 362When you set 'undolevels' to -1 the undo information is not immediately 363cleared, this happens at the next change. To force clearing the undo 364information you can use these commands: > 365 :let old_undolevels = &undolevels 366 :set undolevels=-1 367 :exe "normal a \<BS>\<Esc>" 368 :let &undolevels = old_undolevels 369 :unlet old_undolevels 370 371Marks for the buffer ('a to 'z) are also saved and restored, together with the 372text. 373 374When all changes have been undone, the buffer is not considered to be changed. 375It is then possible to exit Vim with ":q" instead of ":q!". Note 376that this is relative to the last write of the file. Typing "u" after ":w" 377actually changes the buffer, compared to what was written, so the buffer is 378considered changed then. 379 380When manual |folding| is being used, the folds are not saved and restored. 381Only changes completely within a fold will keep the fold as it was, because 382the first and last line of the fold don't change. 383 384The numbered registers can also be used for undoing deletes. Each time you 385delete text, it is put into register "1. The contents of register "1 are 386shifted to "2, etc. The contents of register "9 are lost. You can now get 387back the most recent deleted text with the put command: '"1P'. (also, if the 388deleted text was the result of the last delete or copy operation, 'P' or 'p' 389also works as this puts the contents of the unnamed register). You can get 390back the text of three deletes ago with '"3P'. 391 392 *redo-register* 393If you want to get back more than one part of deleted text, you can use a 394special feature of the repeat command ".". It will increase the number of the 395register used. So if you first do '"1P', the following "." will result in a 396'"2P'. Repeating this will result in all numbered registers being inserted. 397 398Example: If you deleted text with 'dd....' it can be restored with 399 '"1P....'. 400 401If you don't know in which register the deleted text is, you can use the 402:display command. An alternative is to try the first register with '"1P', and 403if it is not what you want do 'u.'. This will remove the contents of the 404first put, and repeat the put command for the second register. Repeat the 405'u.' until you got what you want. 406 407 vim:tw=78:ts=8:noet:ft=help:norl: 408