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