blob: fdfa71df60bb85839935bacd6da44f230e5bf1df [file] [log] [blame]
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001*undo.txt* For Vim version 7.0aa. Last change: 2006 Mar 14
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|
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000144. Undo branches |undo-branches|
155. Remarks about undo |undo-remarks|
Bram Moolenaar071d4272004-06-13 20:20:40 +000016
17==============================================================================
181. Undo and redo commands *undo-commands*
19
20<Undo> or *undo* *<Undo>* *u*
21u Undo [count] changes. {Vi: only one level}
22
23 *:u* *:un* *:undo*
24:u[ndo] Undo one change. {Vi: only one level}
25
26 *CTRL-R*
27CTRL-R Redo [count] changes which were undone. {Vi: redraw
28 screen}
29
30 *:red* *:redo* *redo*
31:red[o] Redo one change which was undone. {Vi: no redo}
32
33 *U*
34U Undo all latest changes on one line. {Vi: while not
35 moved off of it}
36
37The last changes are remembered. You can use the undo and redo commands above
38to revert the text to how it was before each change. You can also apply the
39changes again, getting back the text before the undo.
40
41The "U" command is treated by undo/redo just like any other command. Thus a
42"u" command undoes a "U" command and a 'CTRL-R' command redoes it again. When
43mixing "U", "u" and 'CTRL-R' you will notice that the "U" command will
44restore the situation of a line to before the previous "U" command. This may
45be confusing. Try it out to get used to it.
46The "U" command will always mark the buffer as changed. When "U" changes the
47buffer back to how it was without changes, it is still considered changed.
48Use "u" to undo changes until the buffer becomes unchanged.
49
50==============================================================================
512. Two ways of undo *undo-two-ways*
52
53How undo and redo commands work depends on the 'u' flag in 'cpoptions'.
54There is the Vim way ('u' excluded) and the vi-compatible way ('u' included).
55In the Vim way, "uu" undoes two changes. In the Vi-compatible way, "uu" does
56nothing (undoes an undo).
57
58'u' excluded, the Vim way:
59You can go back in time with the undo command. You can then go forward again
60with the redo command. If you make a new change after the undo command,
61the redo will not be possible anymore.
62
63'u' included, the Vi-compatible way:
64The undo command undoes the previous change, and also the previous undo command.
65The redo command repeats the previous undo command. It does NOT repeat a
66change command, use "." for that.
67
68Examples Vim way Vi-compatible way ~
69"uu" two times undo no-op
70"u CTRL-R" no-op two times undo
71
72Rationale: Nvi uses the "." command instead of CTRL-R. Unfortunately, this
73 is not Vi compatible. For example "dwdwu." in Vi deletes two
74 words, in Nvi it does nothing.
75
76==============================================================================
Bram Moolenaare224ffa2006-03-01 00:01:28 +0000773. Undo blocks *undo-blocks*
78
79One undo command normally undoes a typed command, no matter how many changes
80that command makes. This sequence of undo-able changes forms an undo block.
81Thus if the typed key(s) call a function, all the commands in the function are
82undone together.
83
84If you want to write a function or script that doesn't create a new undoable
85change but joins in with the previous change use this command:
86
87 *:undoj* *:undojoin*
88:undoj[oin] Join further changes with the previous undo block.
89 Warning: Use with care, it may prevent the user from
90 properly undoing changes.
91 {not in Vi}
92
93This is most useful when you need to prompt the user halfway a change. For
94example in a function that calls |getchar()|. Do make sure that there was a
95related change before this that you must join with.
96
97This doesn't work by itself, because the next key press will start a new
98change again. But you can do something like this: >
99
100 :undojoin | delete
101
102After this an "u" command will undo the delete command and the previous
103change.
104
105==============================================================================
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001064. Undo branches *undo-branches*
107
108Above we only discussed one line of undo. But it is also possible to branch
109off. This happens when you undo a few changes and then make a new change.
110The undone changes become a branch. You can go to that branch with the
111following commands.
112
113What matters here is the order in which the changes are made. Undo and redo
114are not considered changes in this context. After each change you have a new
115state of the text.
116
117 *g-*
118g- Go to older text state. With a count repeat that many
119 times. {not in Vi}
120 *:ea* *:earlier*
121:earlier {count} Go to older text state {count} times.
122:earlier {N}s Go to older text state about {N} seconds before.
123:earlier {N}m Go to older text state about {N} minutes before.
124:earlier {N}h Go to older text state about {N} hours before.
125
126 *g+*
127g+ Go to newer text state. With a count repeat that many
128 times. {not in Vi}
129 *:lat* *:later*
130:later {count} Go to newer text state {count} times.
131:later {N}s Go to newer text state about {N} seconds later.
132:later {N}m Go to newer text state about {N} minutes later.
133:later {N}h Go to newer text state about {N} hours later.
134
135Note that text states will become unreachable when undo information is cleared
136for 'undolevels'.
137
138Don't be surprised when moving through time shows multiple changes to take
139place at a time. This happens when moving through the undo tree and then
140making a new change.
141
142EXAMPLE
143
144Start with this text:
145 one two three ~
146
147Delete the first word by pressing "x" three times:
148 ne two three ~
149 e two three ~
150 two three ~
151
152Now undo that by pressing "u" three times:
153 e two three ~
154 ne two three ~
155 one two three ~
156
157Delete the second word by pressing "x" three times:
158 one wo three ~
159 one o three ~
160 one three ~
161
162Now undo that by using "g-" three times:
163 one o three ~
164 one wo three ~
165 one two three ~
166
167Continue going back in time by pressing "g-" one more time:
168 two three ~
169
170You are now back in the first undo branch, after deleting "one". Repeating
171"g-" will now bring you back to the original text:
172 e two three ~
173 ne two three ~
174 one two three ~
175
176Jump to the last change with ":later 1h":
177 one three ~
178
179And back to the start again with ":earlier 1h":
180 one two three ~
181
182
183Note that using "u" and CTRL-R will not get you to all possible text states
184while repeating "g-" and "g+" does.
185
186==============================================================================
1875. Remarks about undo *undo-remarks*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
189The number of changes that are remembered is set with the 'undolevels' option.
190If it is zero, the Vi-compatible way is always used. If it is negative no
191undo is possible. Use this if you are running out of memory.
192
193Marks for the buffer ('a to 'z) are also saved and restored, together with the
194text. {Vi does this a little bit different}
195
196When all changes have been undone, the buffer is not considered to be changed.
197It is then possible to exit Vim with ":q" instead of ":q!" {not in Vi}. Note
198that this is relative to the last write of the file. Typing "u" after ":w"
199actually changes the buffer, compared to what was written, so the buffer is
200considered changed then.
201
202When manual |folding| is being used, the folds are not saved and restored.
203Only changes completely within a fold will keep the fold as it was, because
204the first and last line of the fold don't change.
205
206The numbered registers can also be used for undoing deletes. Each time you
207delete text, it is put into register "1. The contents of register "1 are
208shifted to "2, etc. The contents of register "9 are lost. You can now get
209back the most recent deleted text with the put command: '"1P'. (also, if the
210deleted text was the result of the last delete or copy operation, 'P' or 'p'
211also works as this puts the contents of the unnamed register). You can get
212back the text of three deletes ago with '"3P'.
213
214 *redo-register*
215If you want to get back more than one part of deleted text, you can use a
216special feature of the repeat command ".". It will increase the number of the
217register used. So if you first do ""1P", the following "." will result in a
218'"2P'. Repeating this will result in all numbered registers being inserted.
219
220Example: If you deleted text with 'dd....' it can be restored with
221 '"1P....'.
222
223If you don't know in which register the deleted text is, you can use the
224:display command. An alternative is to try the first register with '"1P', and
225if it is not what you want do 'u.'. This will remove the contents of the
226first put, and repeat the put command for the second register. Repeat the
227'u.' until you got what you want.
228
229 vim:tw=78:ts=8:ft=help:norl: