Christian Brabandt | b4ddc6c | 2024-01-02 16:51:11 +0100 | [diff] [blame] | 1 | *usr_32.txt* For Vim version 9.1. Last change: 2010 Jul 20 |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 2 | |
| 3 | VIM USER MANUAL - by Bram Moolenaar |
| 4 | |
| 5 | The undo tree |
| 6 | |
| 7 | |
| 8 | Vim provides multi-level undo. If you undo a few changes and then make a new |
| 9 | change you create a branch in the undo tree. This text is about moving |
| 10 | through the branches. |
| 11 | |
Bram Moolenaar | 730cde9 | 2010-06-27 05:18:54 +0200 | [diff] [blame] | 12 | |32.1| Undo up to a file write |
| 13 | |32.2| Numbering changes |
| 14 | |32.3| Jumping around the tree |
| 15 | |32.4| Time travelling |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 16 | |
| 17 | Next chapter: |usr_40.txt| Make new commands |
| 18 | Previous chapter: |usr_31.txt| Exploiting the GUI |
| 19 | Table of contents: |usr_toc.txt| |
| 20 | |
| 21 | ============================================================================== |
Bram Moolenaar | 730cde9 | 2010-06-27 05:18:54 +0200 | [diff] [blame] | 22 | *32.1* Undo up to a file write |
| 23 | |
| 24 | Sometimes you make several changes, and then discover you want to go back to |
| 25 | when you have last written the file. You can do that with this command: > |
| 26 | |
| 27 | :earlier 1f |
| 28 | |
| 29 | The "f" stands for "file" here. |
| 30 | |
| 31 | You can repeat this command to go further back in the past. Or use a count |
Bram Moolenaar | fcb7ab6 | 2010-07-20 11:16:17 +0200 | [diff] [blame] | 32 | different from 1 to go back faster. |
Bram Moolenaar | 730cde9 | 2010-06-27 05:18:54 +0200 | [diff] [blame] | 33 | |
| 34 | If you go back too far, go forward again with: > |
| 35 | |
| 36 | :later 1f |
| 37 | |
| 38 | Note that these commands really work in time sequence. This matters if you |
| 39 | made changes after undoing some changes. It's explained in the next section. |
| 40 | |
| 41 | Also note that we are talking about text writes here. For writing the undo |
| 42 | information in a file see |undo-persistence|. |
| 43 | |
| 44 | ============================================================================== |
| 45 | *32.2* Numbering changes |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 46 | |
| 47 | In section |02.5| we only discussed one line of undo/redo. But it is also |
| 48 | possible to branch off. This happens when you undo a few changes and then |
| 49 | make a new change. The new changes become a branch in the undo tree. |
| 50 | |
| 51 | Let's start with the text "one". The first change to make is to append |
| 52 | " too". And then move to the first 'o' and change it into 'w'. We then have |
| 53 | two changes, numbered 1 and 2, and three states of the text: |
| 54 | |
| 55 | one ~ |
| 56 | | |
| 57 | change 1 |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 58 | | |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 59 | one too ~ |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 60 | | |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 61 | change 2 |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 62 | | |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 63 | one two ~ |
| 64 | |
| 65 | If we now undo one change, back to "one too", and change "one" to "me" we |
| 66 | create a branch in the undo tree: |
| 67 | |
| 68 | one ~ |
| 69 | | |
| 70 | change 1 |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 71 | | |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 72 | one too ~ |
| 73 | / \ |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 74 | change 2 change 3 |
| 75 | | | |
| 76 | one two me too ~ |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 77 | |
| 78 | You can now use the |u| command to undo. If you do this twice you get to |
| 79 | "one". Use |CTRL-R| to redo, and you will go to "one too". One more |CTRL-R| |
| 80 | takes you to "me too". Thus undo and redo go up and down in the tree, using |
| 81 | the branch that was last used. |
| 82 | |
| 83 | What matters here is the order in which the changes are made. Undo and redo |
| 84 | are not considered changes in this context. After each change you have a new |
| 85 | state of the text. |
| 86 | |
| 87 | Note that only the changes are numbered, the text shown in the tree above has |
| 88 | no identifier. They are mostly referred to by the number of the change above |
| 89 | it. But sometimes by the number of one of the changes below it, especially |
| 90 | when moving up in the tree, so that you know which change was just undone. |
| 91 | |
| 92 | ============================================================================== |
Bram Moolenaar | 730cde9 | 2010-06-27 05:18:54 +0200 | [diff] [blame] | 93 | *32.3* Jumping around the tree |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 94 | |
| 95 | So how do you get to "one two" now? You can use this command: > |
| 96 | |
| 97 | :undo 2 |
| 98 | |
| 99 | The text is now "one two", you are below change 2. You can use the |:undo| |
| 100 | command to jump to below any change in the tree. |
| 101 | |
| 102 | Now make another change: change "one" to "not": |
| 103 | |
| 104 | one ~ |
| 105 | | |
| 106 | change 1 |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 107 | | |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 108 | one too ~ |
| 109 | / \ |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 110 | change 2 change 3 |
| 111 | | | |
| 112 | one two me too ~ |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 113 | | |
| 114 | change 4 |
| 115 | | |
| 116 | not two ~ |
| 117 | |
| 118 | Now you change your mind and want to go back to "me too". Use the |g-| |
| 119 | command. This moves back in time. Thus it doesn't walk the tree upwards or |
| 120 | downwards, but goes to the change made before. |
| 121 | |
| 122 | You can repeat |g-| and you will see the text change: |
| 123 | me too ~ |
| 124 | one two ~ |
| 125 | one too ~ |
| 126 | one ~ |
| 127 | |
| 128 | Use |g+| to move forward in time: |
| 129 | one ~ |
| 130 | one too ~ |
| 131 | one two ~ |
| 132 | me too ~ |
| 133 | not two ~ |
| 134 | |
| 135 | Using |:undo| is useful if you know what change you want to jump to. |g-| and |
| 136 | |g+| are useful if you don't know exactly what the change number is. |
| 137 | |
| 138 | You can type a count before |g-| and |g+| to repeat them. |
| 139 | |
| 140 | ============================================================================== |
Bram Moolenaar | 730cde9 | 2010-06-27 05:18:54 +0200 | [diff] [blame] | 141 | *32.4* Time travelling |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 142 | |
| 143 | When you have been working on text for a while the tree grows to become big. |
| 144 | Then you may want to go to the text of some minutes ago. |
| 145 | |
| 146 | To see what branches there are in the undo tree use this command: > |
| 147 | |
| 148 | :undolist |
| 149 | < number changes time ~ |
| 150 | 3 2 16 seconds ago |
| 151 | 4 3 5 seconds ago |
| 152 | |
| 153 | Here you can see the number of the leaves in each branch and when the change |
| 154 | was made. Assuming we are below change 4, at "not two", you can go back ten |
| 155 | seconds with this command: > |
| 156 | |
| 157 | :earlier 10s |
| 158 | |
| 159 | Depending on how much time you took for the changes you end up at a certain |
Bram Moolenaar | 730cde9 | 2010-06-27 05:18:54 +0200 | [diff] [blame] | 160 | position in the tree. The |:earlier| command argument can be "m" for minutes, |
| 161 | "h" for hours and "d" for days. To go all the way back use a big number: > |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 162 | |
Bram Moolenaar | 730cde9 | 2010-06-27 05:18:54 +0200 | [diff] [blame] | 163 | :earlier 100d |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 164 | |
| 165 | To travel forward in time again use the |:later| command: > |
| 166 | |
| 167 | :later 1m |
| 168 | |
| 169 | The arguments are "s", "m" and "h", just like with |:earlier|. |
| 170 | |
Bram Moolenaar | 730cde9 | 2010-06-27 05:18:54 +0200 | [diff] [blame] | 171 | If you want even more details, or want to manipulate the information, you can |
| 172 | use the |undotree()| function. To see what it returns: > |
| 173 | |
| 174 | :echo undotree() |
| 175 | |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 176 | ============================================================================== |
| 177 | |
| 178 | Next chapter: |usr_40.txt| Make new commands |
| 179 | |
Bram Moolenaar | d473c8c | 2018-08-11 18:00:22 +0200 | [diff] [blame] | 180 | Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: |