Undo tree and Gundo

Most modern editors support an undo stack, with undoing and replaying operations. Vim takes that one step further by introducing an undo tree. If you make a change, X, undo it, and then make a change, Y—Vim still saves the change, X. Vim supports manually browsing undo tree leaves, but there's a better way to do this.

Gundo is a plugin that visualizes the undo tree and is available from GitHub at: https://github.com/sjl/gundo.vim.git.

If you're using vim-plug to manage your plugins, add the following to your .vimrc file: Plug 'sjl/gundo.vim'. Execute :w | so $MYVIMRC | PlugInstall and you'll have Gundo installed and ready to go.

Let's say you're working on animal_farm.py, with your cursor on line 15:

You're editing the highlighted line: if kind == 'cat'. You perform the following operations:

  1. Replace the cat text with leopard
  2. Undo the edit using the undo command (u)
  3. Replace the cat text with lion

Normally, you'd expect the edit where you changed cat to leopard to be lost, but since Vim has an undo tree, the change is preserved!

Executing :GundoToggle will open two new windows in a split: the visual representation of the tree (top-left) and the difference between that version and a previous snapshot (bottom-left). Here's how it looks:

With your cursor over the Gundo window, you can navigate up and down the the tree with j and kGo to the last change at the top of the tree if you're not already on it (you can use gg to quickly make your way to the top of the buffer). You can see how we changed the line containing cat to contain lion in our last change (lines starting with - signify a removal of the line, while lines starting with + show the addition of a line).

Now, hit j to go down the tree to a different (now unused) branch:

This is the edit we thought we had lost! You can see how we replaced the word cat with leopard. Hit Enter, and Gundo will restore the edit!

Run :GundoToggle again to hide the undo tree.

If you're anything like me, you'll use the Gundo tree a lot. I have it mapped to the F5 key, to make it easier to invoke:

noremap <f5> :GundoToggle<cr>  " Map Gundo to <F5>.
If you want to learn more about the undo tree, see :help undo-tree.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.106.69