Undo as a Tree
g- and g+ walk by time. :undolist shows the branches.
Vim's undo is a tree. Every undo branch is preserved. g- and g+ walk by chronological time;:undolistshows the branches;:earlierand:laterjump through time.
Ordinary u and Ctrl-R are enough until history branches. The branch appears when you undo several changes and then make a different change. Many editors discard the old redo path at that moment. Vim keeps both futures.
That design extends the fearless editing model. A speculative edit does not have to destroy the path you abandoned. Vim records the new experiment as another branch, and the chronological commands can still reach the older states.
| Key or command | Action |
|---|---|
| u | Undo the previous change along the current branch |
| Ctrl-R | Redo along the current branch |
| g- | Go to an older state by chronological time |
| g+ | Go to a newer state by chronological time |
:undolist |
Show the undo branches |
:earlier 5m |
Restore the state from five minutes earlier |
:later 30s |
Move forward by thirty seconds |
:earlier 10 |
Move backward by ten changes |
Persistent Undo
With undofile enabled, Vim saves undo history when the file is written and restores it in a later session. Neovim enables persistent undo only when your configuration turns the option on. The undodir option controls where those undo files live.
Watch
See also: Undo and Redo