Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | *undo.txt* For Vim version 7.0aa. Last change: 2003 Oct 21 |
| 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 | 1. Undo and redo commands |undo-commands| |
| 12 | 2. Two ways of undo |undo-two-ways| |
| 13 | 3. Remarks about undo |undo-remarks| |
| 14 | |
| 15 | ============================================================================== |
| 16 | 1. Undo and redo commands *undo-commands* |
| 17 | |
| 18 | <Undo> or *undo* *<Undo>* *u* |
| 19 | u Undo [count] changes. {Vi: only one level} |
| 20 | |
| 21 | *:u* *:un* *:undo* |
| 22 | :u[ndo] Undo one change. {Vi: only one level} |
| 23 | |
| 24 | *CTRL-R* |
| 25 | CTRL-R Redo [count] changes which were undone. {Vi: redraw |
| 26 | screen} |
| 27 | |
| 28 | *:red* *:redo* *redo* |
| 29 | :red[o] Redo one change which was undone. {Vi: no redo} |
| 30 | |
| 31 | *U* |
| 32 | U Undo all latest changes on one line. {Vi: while not |
| 33 | moved off of it} |
| 34 | |
| 35 | The last changes are remembered. You can use the undo and redo commands above |
| 36 | to revert the text to how it was before each change. You can also apply the |
| 37 | changes again, getting back the text before the undo. |
| 38 | |
| 39 | The "U" command is treated by undo/redo just like any other command. Thus a |
| 40 | "u" command undoes a "U" command and a 'CTRL-R' command redoes it again. When |
| 41 | mixing "U", "u" and 'CTRL-R' you will notice that the "U" command will |
| 42 | restore the situation of a line to before the previous "U" command. This may |
| 43 | be confusing. Try it out to get used to it. |
| 44 | The "U" command will always mark the buffer as changed. When "U" changes the |
| 45 | buffer back to how it was without changes, it is still considered changed. |
| 46 | Use "u" to undo changes until the buffer becomes unchanged. |
| 47 | |
| 48 | ============================================================================== |
| 49 | 2. Two ways of undo *undo-two-ways* |
| 50 | |
| 51 | How undo and redo commands work depends on the 'u' flag in 'cpoptions'. |
| 52 | There is the Vim way ('u' excluded) and the vi-compatible way ('u' included). |
| 53 | In the Vim way, "uu" undoes two changes. In the Vi-compatible way, "uu" does |
| 54 | nothing (undoes an undo). |
| 55 | |
| 56 | 'u' excluded, the Vim way: |
| 57 | You can go back in time with the undo command. You can then go forward again |
| 58 | with the redo command. If you make a new change after the undo command, |
| 59 | the redo will not be possible anymore. |
| 60 | |
| 61 | 'u' included, the Vi-compatible way: |
| 62 | The undo command undoes the previous change, and also the previous undo command. |
| 63 | The redo command repeats the previous undo command. It does NOT repeat a |
| 64 | change command, use "." for that. |
| 65 | |
| 66 | Examples Vim way Vi-compatible way ~ |
| 67 | "uu" two times undo no-op |
| 68 | "u CTRL-R" no-op two times undo |
| 69 | |
| 70 | Rationale: Nvi uses the "." command instead of CTRL-R. Unfortunately, this |
| 71 | is not Vi compatible. For example "dwdwu." in Vi deletes two |
| 72 | words, in Nvi it does nothing. |
| 73 | |
| 74 | ============================================================================== |
| 75 | 3. Remarks about undo *undo-remarks* |
| 76 | |
| 77 | The number of changes that are remembered is set with the 'undolevels' option. |
| 78 | If it is zero, the Vi-compatible way is always used. If it is negative no |
| 79 | undo is possible. Use this if you are running out of memory. |
| 80 | |
| 81 | Marks for the buffer ('a to 'z) are also saved and restored, together with the |
| 82 | text. {Vi does this a little bit different} |
| 83 | |
| 84 | When all changes have been undone, the buffer is not considered to be changed. |
| 85 | It is then possible to exit Vim with ":q" instead of ":q!" {not in Vi}. Note |
| 86 | that this is relative to the last write of the file. Typing "u" after ":w" |
| 87 | actually changes the buffer, compared to what was written, so the buffer is |
| 88 | considered changed then. |
| 89 | |
| 90 | When manual |folding| is being used, the folds are not saved and restored. |
| 91 | Only changes completely within a fold will keep the fold as it was, because |
| 92 | the first and last line of the fold don't change. |
| 93 | |
| 94 | The numbered registers can also be used for undoing deletes. Each time you |
| 95 | delete text, it is put into register "1. The contents of register "1 are |
| 96 | shifted to "2, etc. The contents of register "9 are lost. You can now get |
| 97 | back the most recent deleted text with the put command: '"1P'. (also, if the |
| 98 | deleted text was the result of the last delete or copy operation, 'P' or 'p' |
| 99 | also works as this puts the contents of the unnamed register). You can get |
| 100 | back the text of three deletes ago with '"3P'. |
| 101 | |
| 102 | *redo-register* |
| 103 | If you want to get back more than one part of deleted text, you can use a |
| 104 | special feature of the repeat command ".". It will increase the number of the |
| 105 | register used. So if you first do ""1P", the following "." will result in a |
| 106 | '"2P'. Repeating this will result in all numbered registers being inserted. |
| 107 | |
| 108 | Example: If you deleted text with 'dd....' it can be restored with |
| 109 | '"1P....'. |
| 110 | |
| 111 | If you don't know in which register the deleted text is, you can use the |
| 112 | :display command. An alternative is to try the first register with '"1P', and |
| 113 | if it is not what you want do 'u.'. This will remove the contents of the |
| 114 | first put, and repeat the put command for the second register. Repeat the |
| 115 | 'u.' until you got what you want. |
| 116 | |
| 117 | vim:tw=78:ts=8:ft=help:norl: |