The last news item on vim.org talks about using the 'undofile' to save your history and undo changes even after a reboot:

A feature I enjoy using myself is not known to many users, as I found out last weekend. Besides undo with as many levels as you like, Vim also offers storing the undo information in a file. So you can exit Vim, reboot your computer and still undo changes you made. See the help for 'undofile'. (Bram Moolenaar)

How can I enable and use this feature?


1 Answer 1


The simplest version is: create ~/.vim/undo-dir/ directory and add the following to the .vimrc:

set undodir=~/.vim/undo-dir
set undofile

However, it's not flawless, and it's better to read on. From my own .vimrc:

" Let's save undo info!
if !isdirectory($HOME."/.vim")
    call mkdir($HOME."/.vim", "", 0770)
if !isdirectory($HOME."/.vim/undo-dir")
    call mkdir($HOME."/.vim/undo-dir", "", 0700)
set undodir=~/.vim/undo-dir
set undofile

I prefer not to clutter random directories with undo files. So I set the undodir option, which puts all of them in one undo directory. That leaves one problem—I'm lazy, and don't feel like creating that directory on all my machines (I use a version control system to sync my .vimrc across a bunch of machines). Thankfully, it's easy enough to have Vim check for and create the directories if they don't exist.

Note also the 0700 mode on the undo directory (that's full permissions for the owner, no permissions for anyone else). Which files I've edited, when, etc. should stay private. And since this is a directory only I can access, I don't have to worry about someone else putting a symlink in there, or their own undo file, or whatever as you'd have to worry about using a system temporary directory.

Finally, not being a system temporary directory, it's not subject to the system tempfile cleanup policy. Often, that's fairly short—a week or so. But to keep it from growing forever, I put the following in my crontab:

# m h  dom mon dow   command
 43 00 *   *   3     find /home/anthony/.vim/undo-dir -type f -mtime +90 -delete

So after 90 days of not being modified, they're deleted. (Honestly, they're not really that big, and you could easily leave them forever with how cheap disk is. But consider also your own privacy if your machine is ever compromised.)

  • Thanks for the detailed answer @derobert! I know this is an old question, but I've been trying to come up with a vimscript way of deleting old vim undo files (vi.stackexchange.com/questions/16053/…). I think it'd be cleaner to do it that way, but it's proving not to be easy. Did you ever think of using a vimscript to delete the old vim files? If not, would you still use crontab over a vimscript?
    – Logan
    Commented Apr 26, 2018 at 22:10
  • 1
    @Logan Using a vim script would only work when vim is running, and also what if there are multiple vims running? That cron line is simple, doesn't suffer from those problems, and clearly correct...
    – derobert
    Commented Apr 27, 2018 at 0:40
  • 2
    It seems that mkdir() function supports creating multiple directory structures with the "p" option for {path} parameter. So the above script can be simplified a bit. Using call mkdir($HOME."/.vim/undo-dir", "p", 0700) works for me in Vim 7.4.160 at least.
    – jdhao
    Commented Oct 26, 2019 at 3:20
  • @jdhao Thanks for the shortcut! Although one should be aware of the side effect that ~/.vim will be created with 0700 permissions if it doesn't exist, instead of 0770 as originally specified by derobert. Commented Oct 12, 2020 at 21:54
  • 2
    Thanks. Note for neovim users: just set undofile should be enough. Commented Nov 24, 2020 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.