Learn Vim at your own pace with my self-study Core Vim Course.

Learn more

Learn Vim at your own pace with my self-study Core Vim Course.

Undo branching and Gundo.vim

#30

Run time:

The undo command is almost universally available in software today. Being able to rollback unwanted changes can be a real timesaver. In most applications you can only go backwards and forwards linearly, but Vim keeps your changes in a tree structure, which means that you can retrieve edits from distant branches in your history. This episode will show you how.

Shownotes

Vim maintains your undo history in a tree structure. This is a powerful feature, which lets you switch back to any previous state of your document (up to the value of undolevels), but it’s not easy to understand. Vim’s interface to the undo tree is made up of the familiar undo and redo operations, as well as a handful of ways to traverse the list of changes in chronological order. These commands enable you to shine a light on every corner of the undo tree, but it can still be difficult to get a sense of what you are looking at. Nodes that are adjacent in chronological order may be several steps apart on the graph.

A much more intuitive interface to the undo tree is now available if you install the excellent Gundo plugin, by Steve Losh. This draws an ASCII representation of the undo tree, which you can easily navigate with regular Vim motion commands. Pressing the enter key when your cursor is positioned on a node will revert the document to the specified state.

I recommend installing the Gundo plugin. Being able to retrieve any state from your undo history with confidence will change the way you work. But I would still encourage you to try and understand the way that Vim’s undo tree works. Read the user guide and the documentation. Watch the video, and play with the interactive visualization on this page to get a feel for how Vim’s commands allow you to traverse the graph. You’ll need it one day.

The undo and redo commands can move forwards and backwards along the mainline branch (represented in the visualisation as a solid dark line), but they are not able to switch between branches. When you step along a single track with undo and redo, each update corresponds to a coherent edit, so you can follow the evolution of your document.

The g- and g+ commands traverse the list of historical states of the document. This means that they are capable of switching from one undo branch to another. Beware that jumping between branches can be disorienting, just like time travel. Nevertheless, this powerful feature means that you are never barred from revisiting an earlier state of your document.

command action
u undo the last change
ctrl-r redo one change which was undone
g- go to older text state
g+ go to newer text state
:earlier {N}s Go the the older text state about {N} seconds before

Note that the document timeline records a time-stamp for each historical state, which makes it possible to revert the document to how it looked a particular number of seconds, minutes, hours or days before the current state. I can assure you, running :earlier 20s has saved my bacon on more than one occasion!

Further reading

Comments

Browse similar content


Level-up your Vim

Training

Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify

Publications

Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..