The simplest version is: create
~/.vim/undo-dir/directory and add the following to the.vimrc:However, it’s not flawless, and it’s better to read on. From my own .vimrc:
I prefer not to clutter random directories with undo files. So I set the
undodiroption, 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.vimrcacross 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
0700mode 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: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.)