The most simple version is: create
~/.vim/undo-dir/ directory and add the following to the
However, it's not flawless, and it's better to read on. From my own
" Let's save undo info!
call mkdir($HOME."/.vim", "", 0770)
call mkdir($HOME."/.vim/undo-dir", "", 0700)
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
# 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.)