Skip to main content

Persistent 'undo' in ViM

I love ViM. It's universality, simplicity and keyboard-only-controllability appeals to me tremendously. I hear Emacs is better, but I can't be bothered to learn it. ViM suits me just fine.
However, there's one ViM shortcoming that has annoyed me endlessly. It's inability to maintain edit history across buffer switches. To put it simply, it maintains the edit history that allows undo's and redo's only for the current buffer being worked on. The moment you switch to another buffer, the history is cleaned up and a blank one is given to the new file.
Well, it turns out that ViM has the capability to preserve edit history for each file across buffer switches. Like many other features of the editor, you just gotta turn it on from the rc file. Here's how you do it
set undofile
set undodir=$HOME/.vim/undo
set undolevels=1000
set undoreload=10000
With this in your rc file, ViM creates a temporary file for each file being edited in the folder pointed to by undodir. Make sure you create this folder before you start ViM. ViM won't create it for you, if it doesn't already exist.
Note that undoreload=10000 results in the entire file being edited to be duplicated to theundodir. So if you plan to edit large files with ViM, beware!
Lastly, since the edit history is stored in a file, it'll be available across program sessions. Isn't it awesome?
Thanks to this wonderful answer on SO for the tip.