Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redoing after an accidental change in Vim

Tags:

undo

vim

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?

like image 845
user2958725 Avatar asked Nov 21 '13 15:11

user2958725


People also ask

How do you redo changes in Vim?

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.

How would you undo and redo the last change made in a shell script?

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.

How many times can you use the Vim undo command?

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.


2 Answers

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.

Plugin recommendations

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.

like image 117
Ingo Karkat Avatar answered Nov 15 '22 09:11

Ingo Karkat


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.

like image 38
Mark Wilkins Avatar answered Nov 15 '22 09:11

Mark Wilkins