blob: 433fc759a044d398d6edf2abe4a511689d255275 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001*undo.txt* For Vim version 7.0aa. Last change: 2003 Oct 21
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. Remarks about undo |undo-remarks|
14
15==============================================================================
161. Undo and redo commands *undo-commands*
17
18<Undo> or *undo* *<Undo>* *u*
19u 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*
25CTRL-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*
32U Undo all latest changes on one line. {Vi: while not
33 moved off of it}
34
35The last changes are remembered. You can use the undo and redo commands above
36to revert the text to how it was before each change. You can also apply the
37changes again, getting back the text before the undo.
38
39The "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
41mixing "U", "u" and 'CTRL-R' you will notice that the "U" command will
42restore the situation of a line to before the previous "U" command. This may
43be confusing. Try it out to get used to it.
44The "U" command will always mark the buffer as changed. When "U" changes the
45buffer back to how it was without changes, it is still considered changed.
46Use "u" to undo changes until the buffer becomes unchanged.
47
48==============================================================================
492. Two ways of undo *undo-two-ways*
50
51How undo and redo commands work depends on the 'u' flag in 'cpoptions'.
52There is the Vim way ('u' excluded) and the vi-compatible way ('u' included).
53In the Vim way, "uu" undoes two changes. In the Vi-compatible way, "uu" does
54nothing (undoes an undo).
55
56'u' excluded, the Vim way:
57You can go back in time with the undo command. You can then go forward again
58with the redo command. If you make a new change after the undo command,
59the redo will not be possible anymore.
60
61'u' included, the Vi-compatible way:
62The undo command undoes the previous change, and also the previous undo command.
63The redo command repeats the previous undo command. It does NOT repeat a
64change command, use "." for that.
65
66Examples Vim way Vi-compatible way ~
67"uu" two times undo no-op
68"u CTRL-R" no-op two times undo
69
70Rationale: 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==============================================================================
753. Remarks about undo *undo-remarks*
76
77The number of changes that are remembered is set with the 'undolevels' option.
78If it is zero, the Vi-compatible way is always used. If it is negative no
79undo is possible. Use this if you are running out of memory.
80
81Marks for the buffer ('a to 'z) are also saved and restored, together with the
82text. {Vi does this a little bit different}
83
84When all changes have been undone, the buffer is not considered to be changed.
85It is then possible to exit Vim with ":q" instead of ":q!" {not in Vi}. Note
86that this is relative to the last write of the file. Typing "u" after ":w"
87actually changes the buffer, compared to what was written, so the buffer is
88considered changed then.
89
90When manual |folding| is being used, the folds are not saved and restored.
91Only changes completely within a fold will keep the fold as it was, because
92the first and last line of the fold don't change.
93
94The numbered registers can also be used for undoing deletes. Each time you
95delete text, it is put into register "1. The contents of register "1 are
96shifted to "2, etc. The contents of register "9 are lost. You can now get
97back the most recent deleted text with the put command: '"1P'. (also, if the
98deleted text was the result of the last delete or copy operation, 'P' or 'p'
99also works as this puts the contents of the unnamed register). You can get
100back the text of three deletes ago with '"3P'.
101
102 *redo-register*
103If you want to get back more than one part of deleted text, you can use a
104special feature of the repeat command ".". It will increase the number of the
105register 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
108Example: If you deleted text with 'dd....' it can be restored with
109 '"1P....'.
110
111If 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
113if it is not what you want do 'u.'. This will remove the contents of the
114first 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: