neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

usr_32.txt (5336B)


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