undo.txt (17178B)
1 *undo.txt* Nvim 2 3 4 VIM REFERENCE MANUAL by Bram Moolenaar 5 6 7 Undo and redo *undo-redo* 8 9 The basics are explained in section |02.5| of the user manual. 10 11 Type |gO| to see the table of contents. 12 13 ============================================================================== 14 1. Undo and redo commands *undo-commands* 15 16 <Undo> or *undo* *<Undo>* *u* 17 u Undo [count] changes. 18 19 *:u* *:un* *:undo* 20 :u[ndo] Undo one change. 21 *E830* 22 :u[ndo] {N} Jump to after change number {N}. See |undo-branches| 23 for the meaning of {N}. 24 25 :u[ndo]! Undo one change and remove it from undo history. 26 *E5767* 27 :u[ndo]! {N} Like ":u[ndo] {N}", but forget all changes in the 28 current undo branch up until {N}. You may only use 29 ":undo! {N}" to move backwards in the same undo 30 branch, not to redo or switch to a different undo 31 branch. 32 33 *CTRL-R* 34 CTRL-R Redo [count] changes which were undone. 35 36 *:red* *:redo* *redo* 37 :red[o] Redo one change which was undone. 38 39 *U* 40 U Undo all latest changes on one line, the line where 41 the latest change was made. |U| itself also counts as 42 a change, and thus |U| undoes a previous |U|. 43 44 The last changes are remembered. You can use the undo and redo commands above 45 to revert the text to how it was before each change. You can also apply the 46 changes again, getting back the text before the undo. 47 48 The "U" command is treated by undo/redo just like any other command. Thus a 49 "u" command undoes a "U" command and a 'CTRL-R' command redoes it again. When 50 mixing "U", "u" and 'CTRL-R' you will notice that the "U" command will 51 restore the situation of a line to before the previous "U" command. This may 52 be confusing. Try it out to get used to it. 53 The "U" command will always mark the buffer as changed. When "U" changes the 54 buffer back to how it was without changes, it is still considered changed. 55 Use "u" to undo changes until the buffer becomes unchanged. 56 57 ============================================================================== 58 2. Two ways of undo *undo-two-ways* 59 60 How undo and redo commands work depends on the 'u' flag in 'cpoptions'. 61 There is the Vim way ('u' excluded) and the Vi-compatible way ('u' included). 62 In the Vim way, "uu" undoes two changes. In the Vi-compatible way, "uu" does 63 nothing (undoes an undo). 64 65 'u' excluded, the Vim way: 66 You can go back in time with the undo command. You can then go forward again 67 with the redo command. If you make a new change after the undo command, 68 the redo will not be possible anymore. 69 70 'u' included, the Vi-compatible way: 71 The undo command undoes the previous change, and also the previous undo 72 command. The redo command repeats the previous undo command. It does NOT 73 repeat a change command, use "." for that. 74 75 Examples Vim way Vi-compatible way ~ 76 "uu" two times undo no-op 77 "u CTRL-R" no-op two times undo 78 79 Rationale: Nvi uses the "." command instead of CTRL-R. Unfortunately, this 80 is not Vi compatible. For example "dwdwu." in Vi deletes two 81 words, in Nvi it does nothing. 82 83 ============================================================================== 84 3. Undo blocks *undo-blocks* 85 86 One undo command normally undoes a typed command, no matter how many changes 87 that command makes. This sequence of undo-able changes forms an undo block. 88 Thus if the typed key(s) call a function, all the commands in the function are 89 undone together. 90 91 If you want to write a function or script that doesn't create a new undoable 92 change but joins in with the previous change use this command: 93 94 *:undoj* *:undojoin* *E790* 95 :undoj[oin] Join further changes with the previous undo block. 96 Warning: Use with care, it may prevent the user from 97 properly undoing changes. Don't use this after undo 98 or redo. 99 100 This is most useful when you need to prompt the user halfway through a change. 101 For example in a function that calls |getchar()|. Do make sure that there was 102 a related change before this that you must join with. 103 104 This doesn't work by itself, because the next key press will start a new 105 change again. But you can do something like this: > 106 107 :undojoin | delete 108 109 After this a "u" command will undo the delete command and the previous 110 change. 111 *undo-break* *undo-close-block* 112 To do the opposite, use a new undo block for the next change, in Insert mode 113 use CTRL-G u. This is useful if you want an insert command to be undoable in 114 parts. E.g., for each sentence. |i_CTRL-G_u| 115 116 Setting the value of 'undolevels' also closes the undo block. Even when the 117 new value is equal to the old value. Use `g:undolevels` to explicitly read 118 and write only the global value of 'undolevels'. > 119 let &g:undolevels = &g:undolevels 120 121 Note that the similar-looking assignment `let &undolevels=&undolevels` does not 122 preserve the global option value of 'undolevels' in the event that the local 123 option has been set to a different value. For example: > 124 " Start with different global and local values for 'undolevels'. 125 let &g:undolevels = 1000 126 let &l:undolevels = 2000 127 " This assignment changes the global option to 2000: 128 let &undolevels = &undolevels 129 130 ============================================================================== 131 4. Undo branches *undo-branches* *undo-tree* 132 133 Above we only discussed one line of undo/redo. But it is also possible to 134 branch off. This happens when you undo a few changes and then make a new 135 change. The undone changes become a branch. You can go to that branch with 136 the following commands. The undo-tree of a file can be visualized and 137 interactively applied using |undotree-plugin|. 138 139 This is explained in the user manual: |usr_32.txt|. 140 141 *:undol* *:undolist* 142 :undol[ist] List the leafs in the tree of changes. Example: 143 number changes when saved ~ 144 88 88 2010/01/04 14:25:53 145 108 107 08/07 12:47:51 146 136 46 13:33:01 7 147 166 164 3 seconds ago 148 149 The "number" column is the change number. This number 150 continuously increases and can be used to identify a 151 specific undo-able change, see |:undo|. 152 The "changes" column is the number of changes to this 153 leaf from the root of the tree. 154 The "when" column is the date and time when this 155 change was made. The four possible formats are: 156 N seconds ago 157 HH:MM:SS hour, minute, seconds 158 MM/DD HH:MM:SS idem, with month and day 159 YYYY/MM/DD HH:MM:SS idem, with year 160 The "saved" column specifies, if this change was 161 written to disk and which file write it was. This can 162 be used with the |:later| and |:earlier| commands. 163 For more details use the |undotree()| function. 164 165 *g-* 166 g- Go to older text state. With a count repeat that many 167 times. 168 *:ea* *:earlier* 169 :ea[rlier] {count} Go to older text state {count} times. 170 :ea[rlier] {N}s Go to older text state about {N} seconds before. 171 :ea[rlier] {N}m Go to older text state about {N} minutes before. 172 :ea[rlier] {N}h Go to older text state about {N} hours before. 173 :ea[rlier] {N}d Go to older text state about {N} days before. 174 175 :ea[rlier] {N}f Go to older text state {N} file writes before. 176 When changes were made since the last write 177 ":earlier 1f" will revert the text to the state when 178 it was written. Otherwise it will go to the write 179 before that. 180 When at the state of the first file write, or when 181 the file was not written, ":earlier 1f" will go to 182 before the first change. 183 184 *g+* 185 g+ Go to newer text state. With a count repeat that many 186 times. 187 *:lat* *:later* 188 :lat[er] {count} Go to newer text state {count} times. 189 :lat[er] {N}s Go to newer text state about {N} seconds later. 190 :lat[er] {N}m Go to newer text state about {N} minutes later. 191 :lat[er] {N}h Go to newer text state about {N} hours later. 192 :lat[er] {N}d Go to newer text state about {N} days later. 193 194 :lat[er] {N}f Go to newer text state {N} file writes later. 195 When at the state of the last file write, ":later 1f" 196 will go to the newest text state. 197 198 199 Note that text states will become unreachable when undo information is cleared 200 for 'undolevels'. 201 202 Don't be surprised when moving through time shows multiple changes to take 203 place at a time. This happens when moving through the undo tree and then 204 making a new change. 205 206 EXAMPLE 207 208 Start with this text: 209 one two three ~ 210 211 Delete the first word by pressing "x" three times: 212 ne two three ~ 213 e two three ~ 214 two three ~ 215 216 Now undo that by pressing "u" three times: 217 e two three ~ 218 ne two three ~ 219 one two three ~ 220 221 Delete the second word by pressing "x" three times: 222 one wo three ~ 223 one o three ~ 224 one three ~ 225 226 Now undo that by using "g-" three times: 227 one o three ~ 228 one wo three ~ 229 two three ~ 230 231 You are now back in the first undo branch, after deleting "one". Repeating 232 "g-" will now bring you back to the original text: 233 e two three ~ 234 ne two three ~ 235 one two three ~ 236 237 Jump to the last change with ":later 1h": 238 one three ~ 239 240 And back to the start again with ":earlier 1h": 241 one two three ~ 242 243 244 Note that using "u" and CTRL-R will not get you to all possible text states 245 while repeating "g-" and "g+" does. 246 247 ============================================================================== 248 5. Undo persistence *undo-persistence* *persistent-undo* 249 250 When unloading a buffer Vim normally destroys the tree of undos created for 251 that buffer. By setting the 'undofile' option, Vim will automatically save 252 your undo history when you write a file and restore undo history when you edit 253 the file again. 254 255 The 'undofile' option is checked after writing a file, before the BufWritePost 256 autocommands. If you want to control what files to write undo information 257 for, you can use a BufWritePre autocommand: > 258 au BufWritePre /tmp/* setlocal noundofile 259 260 Vim saves undo trees in a separate undo file, one for each edited file, using 261 a simple scheme that maps filesystem paths directly to undo files. Vim will 262 detect if an undo file is no longer synchronized with the file it was written 263 for (with a hash of the file contents) and ignore it when the file was changed 264 after the undo file was written, to prevent corruption. An undo file is also 265 ignored if its owner differs from the owner of the edited file, except when 266 the owner of the undo file is the current user. Set 'verbose' to get a 267 message about that when opening a file. 268 269 Location of the undo files is controlled by the 'undodir' option, by default 270 they are saved to the dedicated directory in the application data folder. 271 272 You can also save and restore undo histories by using ":wundo" and ":rundo" 273 respectively: 274 *:wu* *:wundo* 275 :wu[ndo][!] {file} 276 Write undo history to {file}. 277 When {file} exists and it does not look like an undo file 278 (the magic number at the start of the file is wrong), then 279 this fails, unless the ! was added. 280 If it exists and does look like an undo file it is 281 overwritten. If there is no undo-history, nothing will be 282 written. 283 Implementation detail: Overwriting happens by first deleting 284 the existing file and then creating a new file with the same 285 name. So it is not possible to overwrite an existing undofile 286 in a write-protected directory. 287 288 *:rund* *:rundo* 289 :rund[o] {file} Read undo history from {file}. 290 291 You can use these in autocommands to explicitly specify the name of the 292 history file. E.g.: > 293 294 au BufReadPost * call ReadUndo() 295 au BufWritePost * call WriteUndo() 296 func ReadUndo() 297 if filereadable(expand('%:h') .. '/UNDO/' .. expand('%:t')) 298 rundo %:h/UNDO/%:t 299 endif 300 endfunc 301 func WriteUndo() 302 let dirname = expand('%:h') .. '/UNDO' 303 if !isdirectory(dirname) 304 call mkdir(dirname) 305 endif 306 wundo %:h/UNDO/%:t 307 endfunc 308 309 You should keep 'undofile' off, otherwise you end up with two undo files for 310 every write. 311 312 You can use the |undofile()| function to find out the file name that Vim would 313 use. 314 315 Note that while reading/writing files and 'undofile' is set most errors will 316 be silent, unless 'verbose' is set. With :wundo and :rundo you will get more 317 error messages, e.g., when the file cannot be read or written. 318 319 NOTE: undo files are never deleted by Vim. You need to delete them yourself. 320 321 Reading an existing undo file may fail for several reasons: 322 *E822* It cannot be opened, because the file permissions don't allow it. 323 *E823* The magic number at the start of the file doesn't match. This usually 324 means it is not an undo file. 325 *E824* The version number of the undo file indicates that it's written by a 326 newer version of Vim. You need that newer version to open it. Don't 327 write the buffer if you want to keep the undo info in the file. 328 "File contents changed, cannot use undo info" 329 The file text differs from when the undo file was written. This means 330 the undo file cannot be used, it would corrupt the text. This also 331 happens when 'encoding' differs from when the undo file was written. 332 *E825* The undo file does not contain valid contents and cannot be used. 333 "Not reading undo file, owner differs" 334 The undo file is owned by someone else than the owner of the text 335 file. For safety the undo file is not used. 336 337 Writing an undo file may fail for these reasons: 338 *E828* The file to be written cannot be created. Perhaps you do not have 339 write permissions in the directory. 340 "Cannot write undo file in any directory in 'undodir'" 341 None of the directories in 'undodir' can be used. 342 "Will not overwrite with undo file, cannot read" 343 A file exists with the name of the undo file to be written, but it 344 cannot be read. You may want to delete this file or rename it. 345 "Will not overwrite, this is not an undo file" 346 A file exists with the name of the undo file to be written, but it 347 does not start with the right magic number. You may want to delete 348 this file or rename it. 349 "Skipping undo file write, nothing to undo" 350 There is no undo information to be written, nothing has been changed 351 or 'undolevels' is negative. 352 *E829* An error occurred while writing the undo file. You may want to try 353 again. 354 355 ============================================================================== 356 6. Remarks about undo *undo-remarks* 357 358 The number of changes that are remembered is set with the 'undolevels' option. 359 If it is zero, the Vi-compatible way is always used. If it is negative no 360 undo is possible. Use this if you are running out of memory. 361 362 *clear-undo* 363 When you set 'undolevels' to -1 the undo information is not immediately 364 cleared, this happens at the next change. To force clearing the undo 365 information you can use these commands: > 366 :let old_undolevels = &l:undolevels 367 :setlocal undolevels=-1 368 :exe "normal a \<BS>\<Esc>" 369 :let &l:undolevels = old_undolevels 370 :unlet old_undolevels 371 372 Note use of `&l:undolevels` to explicitly read the local value of 'undolevels' 373 and the use of `:setlocal` to change only the local option (which takes 374 precedence over the corresponding global option value). Saving the option 375 value via the use of `&undolevels` is unpredictable; it reads either the local 376 value (if one has been set) or the global value (otherwise). Also, if a local 377 value has been set, changing the option via `:set undolevels` will change both 378 the global and local values, requiring extra work to save and restore both 379 values. 380 381 Marks for the buffer ('a to 'z) are also saved and restored, together with the 382 text. 383 384 When all changes have been undone, the buffer is not considered to be changed. 385 It is then possible to exit Vim with ":q" instead of ":q!". 386 Note that this is relative to the last write of the file. Typing "u" after 387 ":w" actually changes the buffer, compared to what was written, so the buffer 388 is considered changed then. 389 390 When manual |folding| is being used, the folds are not saved and restored. 391 Only changes completely within a fold will keep the fold as it was, because 392 the first and last line of the fold don't change. 393 394 The numbered registers can also be used for undoing deletes. Each time you 395 delete text, it is put into register "1. The contents of register "1 are 396 shifted to "2, etc. The contents of register "9 are lost. You can now get 397 back the most recent deleted text with the put command: '"1P'. (also, if the 398 deleted text was the result of the last delete or copy operation, 'P' or 'p' 399 also works as this puts the contents of the unnamed register). You can get 400 back the text of three deletes ago with '"3P'. 401 402 *redo-register* 403 If you want to get back more than one part of deleted text, you can use a 404 special feature of the repeat command ".". It will increase the number of the 405 register used. So if you first do '"1P', the following "." will result in a 406 '"2P'. Repeating this will result in all numbered registers being inserted. 407 408 Example: If you deleted text with 'dd....' it can be restored with 409 '"1P....'. 410 411 If you don't know in which register the deleted text is, you can use the 412 :display command. An alternative is to try the first register with '"1P', and 413 if it is not what you want do 'u.'. This will remove the contents of the 414 first put, and repeat the put command for the second register. Repeat the 415 'u.' until you got what you want. 416 417 vim:tw=78:ts=8:noet:ft=help:norl: