blob: bdb7ffbf35d841b27ef14dc65cb23bad7a9fad90 [file] [log] [blame]
Bram Moolenaare224ffa2006-03-01 00:01:28 +00001*undo.txt* For Vim version 7.0aa. Last change: 2006 Feb 28
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
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|
Bram Moolenaare224ffa2006-03-01 00:01:28 +0000133. Undo blocks |undo-blocks|
144. Remarks about undo |undo-remarks|
Bram Moolenaar071d4272004-06-13 20:20:40 +000015
16==============================================================================
171. Undo and redo commands *undo-commands*
18
19<Undo> or *undo* *<Undo>* *u*
20u Undo [count] changes. {Vi: only one level}
21
22 *:u* *:un* *:undo*
23:u[ndo] Undo one change. {Vi: only one level}
24
25 *CTRL-R*
26CTRL-R Redo [count] changes which were undone. {Vi: redraw
27 screen}
28
29 *:red* *:redo* *redo*
30:red[o] Redo one change which was undone. {Vi: no redo}
31
32 *U*
33U Undo all latest changes on one line. {Vi: while not
34 moved off of it}
35
36The last changes are remembered. You can use the undo and redo commands above
37to revert the text to how it was before each change. You can also apply the
38changes again, getting back the text before the undo.
39
40The "U" command is treated by undo/redo just like any other command. Thus a
41"u" command undoes a "U" command and a 'CTRL-R' command redoes it again. When
42mixing "U", "u" and 'CTRL-R' you will notice that the "U" command will
43restore the situation of a line to before the previous "U" command. This may
44be confusing. Try it out to get used to it.
45The "U" command will always mark the buffer as changed. When "U" changes the
46buffer back to how it was without changes, it is still considered changed.
47Use "u" to undo changes until the buffer becomes unchanged.
48
49==============================================================================
502. Two ways of undo *undo-two-ways*
51
52How undo and redo commands work depends on the 'u' flag in 'cpoptions'.
53There is the Vim way ('u' excluded) and the vi-compatible way ('u' included).
54In the Vim way, "uu" undoes two changes. In the Vi-compatible way, "uu" does
55nothing (undoes an undo).
56
57'u' excluded, the Vim way:
58You can go back in time with the undo command. You can then go forward again
59with the redo command. If you make a new change after the undo command,
60the redo will not be possible anymore.
61
62'u' included, the Vi-compatible way:
63The undo command undoes the previous change, and also the previous undo command.
64The redo command repeats the previous undo command. It does NOT repeat a
65change command, use "." for that.
66
67Examples Vim way Vi-compatible way ~
68"uu" two times undo no-op
69"u CTRL-R" no-op two times undo
70
71Rationale: Nvi uses the "." command instead of CTRL-R. Unfortunately, this
72 is not Vi compatible. For example "dwdwu." in Vi deletes two
73 words, in Nvi it does nothing.
74
75==============================================================================
Bram Moolenaare224ffa2006-03-01 00:01:28 +0000763. Undo blocks *undo-blocks*
77
78One undo command normally undoes a typed command, no matter how many changes
79that command makes. This sequence of undo-able changes forms an undo block.
80Thus if the typed key(s) call a function, all the commands in the function are
81undone together.
82
83If you want to write a function or script that doesn't create a new undoable
84change but joins in with the previous change use this command:
85
86 *:undoj* *:undojoin*
87:undoj[oin] Join further changes with the previous undo block.
88 Warning: Use with care, it may prevent the user from
89 properly undoing changes.
90 {not in Vi}
91
92This is most useful when you need to prompt the user halfway a change. For
93example in a function that calls |getchar()|. Do make sure that there was a
94related change before this that you must join with.
95
96This doesn't work by itself, because the next key press will start a new
97change again. But you can do something like this: >
98
99 :undojoin | delete
100
101After this an "u" command will undo the delete command and the previous
102change.
103
104==============================================================================
1054. Remarks about undo *undo-remarks*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
107The number of changes that are remembered is set with the 'undolevels' option.
108If it is zero, the Vi-compatible way is always used. If it is negative no
109undo is possible. Use this if you are running out of memory.
110
111Marks for the buffer ('a to 'z) are also saved and restored, together with the
112text. {Vi does this a little bit different}
113
114When all changes have been undone, the buffer is not considered to be changed.
115It is then possible to exit Vim with ":q" instead of ":q!" {not in Vi}. Note
116that this is relative to the last write of the file. Typing "u" after ":w"
117actually changes the buffer, compared to what was written, so the buffer is
118considered changed then.
119
120When manual |folding| is being used, the folds are not saved and restored.
121Only changes completely within a fold will keep the fold as it was, because
122the first and last line of the fold don't change.
123
124The numbered registers can also be used for undoing deletes. Each time you
125delete text, it is put into register "1. The contents of register "1 are
126shifted to "2, etc. The contents of register "9 are lost. You can now get
127back the most recent deleted text with the put command: '"1P'. (also, if the
128deleted text was the result of the last delete or copy operation, 'P' or 'p'
129also works as this puts the contents of the unnamed register). You can get
130back the text of three deletes ago with '"3P'.
131
132 *redo-register*
133If you want to get back more than one part of deleted text, you can use a
134special feature of the repeat command ".". It will increase the number of the
135register used. So if you first do ""1P", the following "." will result in a
136'"2P'. Repeating this will result in all numbered registers being inserted.
137
138Example: If you deleted text with 'dd....' it can be restored with
139 '"1P....'.
140
141If you don't know in which register the deleted text is, you can use the
142:display command. An alternative is to try the first register with '"1P', and
143if it is not what you want do 'u.'. This will remove the contents of the
144first put, and repeat the put command for the second register. Repeat the
145'u.' until you got what you want.
146
147 vim:tw=78:ts=8:ft=help:norl: