codekingpro/portable-devtools
114k
1*usr_32.txt* For Vim version 9.2. Last change: 2026 Feb 142 3 4 VIM USER MANUAL by Bram Moolenaar5 6 7 The undo tree8 9 10Vim provides multi-level undo. If you undo a few changes and then make a new11change you create a branch in the undo tree. This text is about moving12through the branches.13 14|32.1| Undo up to a file write15|32.2| Numbering changes16|32.3| Jumping around the tree17|32.4| Time travelling18 19 Next chapter: |usr_40.txt| Make new commands20 Previous chapter: |usr_31.txt| Exploiting the GUI21Table of contents: |usr_toc.txt|22 23==============================================================================24*32.1* Undo up to a file write25 26Sometimes you make several changes, and then discover you want to go back to27when you have last written the file. You can do that with this command: >28 29 :earlier 1f30 31The "f" stands for "file" here.32 33You can repeat this command to go further back in the past. Or use a count34different from 1 to go back faster.35 36If you go back too far, go forward again with: >37 38 :later 1f39 40Note that these commands really work in time sequence. This matters if you41made changes after undoing some changes. It's explained in the next section.42 43Also note that we are talking about text writes here. For writing the undo44information in a file see |undo-persistence|.45 46==============================================================================47*32.2* Numbering changes48 49In section |02.5| we only discussed one line of undo/redo. But it is also50possible to branch off. This happens when you undo a few changes and then51make a new change. The new changes become a branch in the undo tree.52 53Let's start with the text "one". The first change to make is to append54" too". And then move to the first 'o' and change it into 'w'. We then have55two changes, numbered 1 and 2, and three states of the text:56 57 one ~58 |59 change 160 |61 one too ~62 |63 change 264 |65 one two ~66 67If we now undo one change, back to "one too", and change "one" to "me" we68create a branch in the undo tree:69 70 one ~71 |72 change 173 |74 one too ~75 / \76 change 2 change 377 | |78 one two me too ~79 80You can now use the |u| command to undo. If you do this twice you get to81"one". Use |CTRL-R| to redo, and you will go to "one too". One more |CTRL-R|82takes you to "me too". Thus undo and redo go up and down in the tree, using83the branch that was last used.84 85What matters here is the order in which the changes are made. Undo and redo86are not considered changes in this context. After each change you have a new87state of the text.88 89Note that only the changes are numbered, the text shown in the tree above has90no identifier. They are mostly referred to by the number of the change above91it. But sometimes by the number of one of the changes below it, especially92when moving up in the tree, so that you know which change was just undone.93 94==============================================================================95*32.3* Jumping around the tree96 97So how do you get to "one two" now? You can use this command: >98 99 :undo 2100 101The text is now "one two", you are below change 2. You can use the |:undo|102command to jump to below any change in the tree.103 104Now make another change: change "one" to "not":105 106 one ~107 |108 change 1109 |110 one too ~111 / \112 change 2 change 3113 | |114 one two me too ~115 |116 change 4117 |118 not two ~119 120Now you change your mind and want to go back to "me too". Use the |g-|121command. This moves back in time. Thus it doesn't walk the tree upwards or122downwards, but goes to the change made before.123 124You can repeat |g-| and you will see the text change:125 me too ~126 one two ~127 one too ~128 one ~129 130Use |g+| to move forward in time:131 one ~132 one too ~133 one two ~134 me too ~135 not two ~136 137Using |:undo| is useful if you know what change you want to jump to. |g-| and138|g+| are useful if you don't know exactly what the change number is.139 140You can type a count before |g-| and |g+| to repeat them.141 142==============================================================================143*32.4* Time travelling144 145When you have been working on text for a while the tree grows to become big.146Then you may want to go to the text of some minutes ago.147 148To see what branches there are in the undo tree use this command: >149 150 :undolist151< number changes time ~152 3 2 16 seconds ago153 4 3 5 seconds ago154 155Here you can see the number of the leaves in each branch and when the change156was made. Assuming we are below change 4, at "not two", you can go back ten157seconds with this command: >158 159 :earlier 10s160 161Depending on how much time you took for the changes you end up at a certain162position in the tree. The |:earlier| command argument can be "m" for minutes,163"h" for hours and "d" for days. To go all the way back use a big number: >164 165 :earlier 100d166 167To travel forward in time again use the |:later| command: >168 169 :later 1m170 171The arguments are "s", "m" and "h", just like with |:earlier|.172 173If you want even more details, or want to manipulate the information, you can174use the |undotree()| function. To see what it returns: >175 176 :echo undotree()177 178==============================================================================179 180Next chapter: |usr_40.txt| Make new commands181 182Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl:183 