Electricmonk

Ferry Boender

Programmer, DevOpper, Open Source enthusiast.

Blog

Persistent undo history in Vim

Thursday, July 26th, 2012

Once you quit Vim, the undo history for that file is gone. This sometimes gives me problems if I accidentally made a change in a file without knowing it. This usually happens due to a bad Vim command which, for instance, capitalized a single letter.

There’s an option which allows you to make the undo history persistent between Vim sessions. That means you can still undo changes you made in a file, even if you’ve quit Vim in the meantime.

You can add the following to your .vimrc to enable it:

set undofile   " Maintain undo history between sessions

This will create undo files all over the place, which look like this:

-rw-r--r-- 1 fboender fboender 320 2012-07-26 10:23 bad_gateway.txt
-rw-r--r-- 1 fboender fboender 523 2012-07-24 14:51 .bad_gateway.txt.un~

You can remedy this by including the following option in your configuration:

set undodir=~/.vim/undodir

Make sure to create the undodir:

$ mkdir ~/.vim/undodir

The undo files will now be saved in the undodir:

$ ls -la .vim/undodir/
total 12
drwxr-xr-x  2 fboender fboender 4096 2012-07-26 10:32 .
drwxr-xr-x 12 fboender fboender 4096 2012-07-26 10:24 ..
-rw-r--r--  1 fboender fboender  519 2012-07-26 10:32 %home%fboender%bad_gateway.txt

Edit: Thanks to Daid Kahl for pointing out the mistake in the comment after the command.

 

The text of all posts on this blog, unless specificly mentioned otherwise, are licensed under this license.