How to avoid problems with Vim undo & redo

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 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;

https://vim.fandom.com/wiki/Undo_and_Redo.

https://vim.fandom.com/wiki/Using_undo_branches

The second of these sites has links to a couple of plugins which make life easier. I’m not generally an enthusiast for plugins but I’ve installed Histwin, which seems pretty good; I’ll probably try Undotree as well.

Finally, thanks to a third page I now know why I occasionally deleted work I’d just done and couldn’t recover it. See:

https://vim.fandom.com/wiki/Recover_from_accidental_Ctrl-U

This explains why it happens (pressing Ctrl-u in Insert mode) and provides a way to avoid it.

 

 

 

Leave a Reply

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

Anti-Spam Quiz: