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