Learning Vim is an endless process. Even after using it for two decades I still keep discovering new settings, features, and plugins that significantly improve my productivity.
The other day I came across “Ask HN: Best things in your bash_profile/aliases?” thread, with plenty of tips and tricks. One particular comment highlighted a feature that I kind of heard about but never got to setting up – persistent undo.
It turns out that starting with Vim 7.3 you can preserve the undo history between editing sessions. Which means that you make changes to a file, save it, close it, and when you reopen it later, you can press ‘u’ to undo the changes you’ve done during the last edit.
In order to set this up, you first need to create a folder, where Vim will store the undo history files. For example:
Then, you need tell Vim that you want to use persistent undo and where to store the files. Edit the .vimrc file and add the following:
set undofile
set undodir=~/.vim/undodir
As long as you are using Vim 7.3 or newer and the directory exists, your persistent undo history will work like a charm.
Read the rest of the thread for more tips on how to clean it up periodically, and how to further improve your experience with Vim’s undo, using plugins that help navigate the undo tree.