36

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?

37

The most simple 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)
endif
if !isdirectory($HOME."/.vim/undo-dir")
    call mkdir($HOME."/.vim/undo-dir", "", 0700)
endif
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, its 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, 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 Apr 26 '18 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 Apr 27 '18 at 0:40
  • 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 Oct 26 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.