How to avoid problems with Vim undo & redo

Last Updated on

Starting with Vim 7.0 this brilliant editor introduced a more powerful set of commands for Undo and Redo, using an undo tree. Being lazy, I didn’t fully get to grips with this and as a result didn’t really understand what I was doing. At times I accidentally deleted text I’d just writtenjjjk and then couldn’t recover it.

I knew the previous version of my work had to be there but I didn’t know how to get it back. After a time I realised that when things seemed to go wrong the solution was usually to use ‘g-‘ and ‘g+’ to go hack and forth in the undo tree.  But this was a bit hit & miss.

If you have had a similar experience here are a couple of useful site where things are explained clearly; Undo and Redo and Using Undo Branches.

Finally, thanks to a third page I now know why I occasionally deleted work I’d just done and couldn’t recover it. See Recover from accidental Ctrl-U. This explains why it happens  and provides a way to recover the deleted text. Better still, it tells you how to prevent it happening in future. Short cut for the impatient:  put these lines in .vimrc:

inoremap <c-u> <c-g>u<c-u>
inoremap <c-w> <c-g>u<c-w>

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Anti-Spam Quiz: