usr_32.txt ForVim version 9.2. Last change: 2026 Feb 14 VIM USER MANUALbyBramMoolenaar Theundo treeVim provides multi-level undo. If youundoa few changes and then makea newchange you createa branch in theundo tree. This textis about movingthrough the branches.32.1 Undo up toa file write32.2 Numbering changes32.3 Jumping around the tree32.4 Time travelling Next chapter:usr_40.txt Make new commands Previous chapter:usr_31.txt Exploiting theGUITable of contents:usr_toc.txt==============================================================================32.1 Undo up toa file writeSometimes you make several changes, and then discover you want togo back towhen you have last written the file. You cando that with this command::earlier 1fThe "f" stands for "file" here.You can repeat this command togo further back in the past. Or useacountdifferent from 1 togo back faster.If yougo back too far,go forward again with::later 1fNote that these commands really work in time sequence. This matters if youmade changes after undoing some changes. It's explained in the next section.Alsonote that we are talking about text writes here. Forwriting theundoinformation ina file seeundo-persistence.==============================================================================32.2 Numbering changesInsection02.5 we only discussed one line of undo/redo. Butitis alsopossible to branch off. This happens when youundoa few changes and thenmakea new change. The new changes becomea branch in theundo tree.Let's start with the text "one". The first change to makeis to append" too". And then move to the first 'o' and changeit into 'w'. We then havetwo changes, numbered 1 and 2, and three states of the text:one | change 1 | one too | change 2 | one twoIf we nowundo one change, back to "one too", and change "one" to "me" wecreatea branch in theundo tree:one | change 1 | one too/ \ change 2 change 3 | | one two me tooYou can now use theu command to undo. If youdo this twice you get to"one". UseCTRL-R to redo, and you willgo to "one too". One moreCTRL-Rtakes you to "me too". Thusundo andredogo up and down in the tree, usingthe branch that was last used.What matters hereis the order in which the changes are made. Undo andredoare not considered changes in this context. After each change you havea newstate of the text.Note that only the changes are numbered, the text shown in the tree above hasno identifier. They are mostly referred to by the number of the change aboveit. But sometimes by the number of one of the changes below it, especiallywhen moving up in the tree, so that you know which change was just undone.==============================================================================32.3 Jumping around the treeSo howdo you get to "one two" now? You can use this command::undo 2The textis now "one two", you are below change 2. You can use the:undocommand to jump to below any change in the tree.Now make another change: change "one" to "not":one | change 1 | one too/ \ change 2 change 3 | | one two me too | change 4 | not twoNow you change your mind and want togo back to "me too". Use theg-command. This moves back in time. Thusit doesn't walk the tree upwards ordownwards, but goes to the change made before.You can repeatg- and you will see the text change:me tooone twoone toooneUseg+ to move forward in time:oneone tooone twome toonot twoUsing:undois useful if you know what change you want to jump to.g- andg+ are useful if you don't know exactly what the change number is.You can typeacount beforeg- andg+ to repeat them.==============================================================================32.4 Time travellingWhen you have been working on text fora while the tree grows to become big.Then you may want togo to the text of some minutes ago.To see what branches there are in theundo tree use this command::undolistnumber changes time 3 2 16 seconds ago 4 3 5 seconds agoHere you can see the number of the leaves in each branch and when the changewas made. Assuming we are below change 4,at "not two", you cango back tenseconds with this command::earlier 10sDepending on how much time you took for the changes youend upata certainposition in the tree. The:earlier command argument can be "m" for minutes,"h" for hours and "d" for days. Togo all the way back usea big number::earlier 100dTo travel forward in time again use the:later command::later 1mThe arguments are "s", "m" and "h", just like with:earlier.If you want even more details, or want to manipulate the information, you canuse theundotree() function. To see whatit returns::echo undotree()==============================================================================Next chapter:usr_40.txt Make new commandsCopyright: seemanual-copyright vim:tw=78:ts=8:noet:ft=help:norl: