Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

ViM: Persistent Undo

📅 2010-Nov-27 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ undo, vim ⬩ 📚 Archive

ViM 7.3 has the awesome new feature of persistent undo. This means that ViM can remember the editing actions done on a file, even after ViM is closed and the file is reopened in ViM later! ViM does this by maintaining an undo file for each file that is edited. Persistent undo is turned off by default.

To turn on persistent undo, add this line to your vimrc file:

set undofile

The command to undo an action is u. This command can now undo actions that were done in earlier ViM sessions on the currently open file. That is some elephant memory now! 😊

Undo Files Directory

When persistent undo is turned on, it creates files named .file.un~ in the directory of the file itself. I find this quite dirty and would like all the undo files to be created and managed in a far-off temporary directory. The undo files directory is set to . (current directory) by default. It can be set to a different place by adding this line to your vimrc file:

set undodir=$TEMP

Here I have set it to the user's Temp directory on Windows. When undodir is set to something other than ., ViM creates undo files in that directory with a different filename convention. The filename is the full path of the edited file, with the : and \ characters replaced by % characters. For example, the undo file for C:\Code\Main.cpp is stored in $TEMP with the filename C%%Code%Main.cpp

I find persistent undo to be an awesome feature that just works as your mind expects it to. Turn it on and enjoy the ViM goodness! 😊