Undo That Outlives the Session

The catch

Close the file and all of it evaporates — the tree, the timestamps, the branches. Reopen and Vim has no memory of any of it.

Unless you tell it to keep them.

Two lines

set undofile
set undodir=~/.vim/undodir
  • undofile — write the undo history to disk on every save
  • undodir — keep those history files in one folder instead of scattering them next to your source

The folder must exist

Vim will not create it for you. From inside the editor:

:!mkdir -p ~/.vim/undodir

The payoff

Edit a file, save, quit. Open it again in a brand new Vim and press u — the change undoes. Vim reports 1 more line; before #1 26 seconds ago.

Close your laptop, come back tomorrow, and still walk backwards through everything you did. Two lines of config.