blob: 9fab186e1858cc7676bc725bdf4eae0f0c8f05df [file] [log] [blame]
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001*usr_32.txt* For Vim version 7.0f. Last change: 2006 Apr 24
Bram Moolenaarc01140a2006-03-24 22:21:52 +00002
3 VIM USER MANUAL - by Bram Moolenaar
4
5 The undo tree
6
7
8Vim provides multi-level undo. If you undo a few changes and then make a new
9change you create a branch in the undo tree. This text is about moving
10through the branches.
11
12|32.1| Numbering changes
13|32.2| Jumping around the tree
14|32.3| Time travelling
15
16 Next chapter: |usr_40.txt| Make new commands
17 Previous chapter: |usr_31.txt| Exploiting the GUI
18Table of contents: |usr_toc.txt|
19
20==============================================================================
21*32.1* Numbering changes
22
23In section |02.5| we only discussed one line of undo/redo. But it is also
24possible to branch off. This happens when you undo a few changes and then
25make a new change. The new changes become a branch in the undo tree.
26
27Let's start with the text "one". The first change to make is to append
28" too". And then move to the first 'o' and change it into 'w'. We then have
29two changes, numbered 1 and 2, and three states of the text:
30
31 one ~
32 |
33 change 1
34 |
35 one too ~
36 |
37 change 2
38 |
39 one two ~
40
41If we now undo one change, back to "one too", and change "one" to "me" we
42create a branch in the undo tree:
43
44 one ~
45 |
46 change 1
47 |
48 one too ~
49 / \
50 change 2 change 3
51 | |
52 one two me too ~
53
54You can now use the |u| command to undo. If you do this twice you get to
55"one". Use |CTRL-R| to redo, and you will go to "one too". One more |CTRL-R|
56takes you to "me too". Thus undo and redo go up and down in the tree, using
57the branch that was last used.
58
59What matters here is the order in which the changes are made. Undo and redo
60are not considered changes in this context. After each change you have a new
61state of the text.
62
63Note that only the changes are numbered, the text shown in the tree above has
64no identifier. They are mostly referred to by the number of the change above
65it. But sometimes by the number of one of the changes below it, especially
66when moving up in the tree, so that you know which change was just undone.
67
68==============================================================================
69*32.2* Jumping around the tree
70
71So how do you get to "one two" now? You can use this command: >
72
73 :undo 2
74
75The text is now "one two", you are below change 2. You can use the |:undo|
76command to jump to below any change in the tree.
77
78Now make another change: change "one" to "not":
79
80 one ~
81 |
82 change 1
83 |
84 one too ~
85 / \
86 change 2 change 3
87 | |
88 one two me too ~
89 |
90 change 4
91 |
92 not two ~
93
94Now you change your mind and want to go back to "me too". Use the |g-|
95command. This moves back in time. Thus it doesn't walk the tree upwards or
96downwards, but goes to the change made before.
97
98You can repeat |g-| and you will see the text change:
99 me too ~
100 one two ~
101 one too ~
102 one ~
103
104Use |g+| to move forward in time:
105 one ~
106 one too ~
107 one two ~
108 me too ~
109 not two ~
110
111Using |:undo| is useful if you know what change you want to jump to. |g-| and
112|g+| are useful if you don't know exactly what the change number is.
113
114You can type a count before |g-| and |g+| to repeat them.
115
116==============================================================================
117*32.3* Time travelling
118
119When you have been working on text for a while the tree grows to become big.
120Then you may want to go to the text of some minutes ago.
121
122To see what branches there are in the undo tree use this command: >
123
124 :undolist
125< number changes time ~
126 3 2 16 seconds ago
127 4 3 5 seconds ago
128
129Here you can see the number of the leaves in each branch and when the change
130was made. Assuming we are below change 4, at "not two", you can go back ten
131seconds with this command: >
132
133 :earlier 10s
134
135Depending on how much time you took for the changes you end up at a certain
136position in the tree. The |:earlier| command argument can be "m" for minutes
137and "h" for hours. To go all the way back use a big number: >
138
139 :earlier 10h
140
141To travel forward in time again use the |:later| command: >
142
143 :later 1m
144
145The arguments are "s", "m" and "h", just like with |:earlier|.
146
147==============================================================================
148
149Next chapter: |usr_40.txt| Make new commands
150
151Copyright: see |manual-copyright| vim:tw=78:ts=8:ft=help:norl: