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