usr_32.txt 5.3 KB

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