ViM: Persistent Undo

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! πŸ™‚

3 thoughts on “ViM: Persistent Undo

    1. Amanda: Yes, one of those hidden gems in Vim. The best part is you turn it on and forget about it. It just works πŸ™‚


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.