Let's say I make 3 changes, C1, C2, and C3.
Then I undo X3. Then I redo X3. I'm back where I started.
Then I undo X3 again, but then I accidentally type ifoo<Esc>
. What can I now do to recover change #3?
To redo in Vim, you need to be in the normal mode (press Esc ). 2. Now you can redo changes you have previously undone – hold Ctrl and press r . Vim will redo the last undone entry.
Type u to undo the last change. To undo the two last changes, you would type 2u . Press Ctrl-r to redo changes which were undone.
2 Answers. Show activity on this post. You have only 1 level of undo if you are in Vi compatible mode. You are missing out on a number of features by being in 'compatible' mode.
Vim is special in that it not just stores a linear history of edits (and undo), but actually all branches! You can use the g-
and g+
commands to move through them, and the :earlier
command to move to text states by count, seconds, minutes, etc. See :help undo-branches
and :help usr_32.txt
for details.
Because that kind of navigation is still mentally taxing (and one doesn't want to get lost in a potentially huge undo tree!), the undotree.vim - Display your undo history in a graph and Gundo - Visualize your undo tree plugins provide a much better visualization, including diffs to see what changed in each state.
Given that sequence, you should be able to type g-
(g-) one time to get back to change 3. You might want to open another instance of Vim and test it to verify it does what you want.
Type :h undo-branches
for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With