I just copied a massive amount of text (on the order of millions of lines, I think) into a Vim register, opened a new file, pasted, and then proceeded to edit the new copy of the excerpt. Vim was reasonably quick throughout the whole process; it was a nice showcase of the power of Vim.

....except that I'm using the persistent undo feature. So after a little bit of time spent editing, I noticed that my Vim session was taking up an enormous amount of memory (about 3x as much as my Chrome session, which is a lot by Vim standards). At first I thought it was because my massive copy was still in one of the registers, but when I went to edit .viminfo, the longest register value was about 20 lines. I tried restarting Vim a few times, but each time it loaded that particular file, it would hang for an unacceptable amount of time (again, by Vim standards).

...So I realized that I needed to delete my undo file for the problematic file. This solved my immediate problem, but I'd like to avoid encountering it again. So I would like an autocmd in my VimRC that will do the following:

  • Upon loading a file, check the size of the corresponding undo file (if it exists)
  • If the undo file is larger than x Mb (or whatever size), do one of the following:
    • Delete it
    • Keeping only the most recent changes, delete changes until the file is a reasonable size (this would be the preferred behavior)

Any thoughts on how to do this?


From undo-persistence:

The 'undofile' option is checked after writing a file, before the BufWritePost
autocommands.  If you want to control what files to write undo information
for, you can use a BufWritePre autocommand: >
    au BufWritePre /tmp/* setlocal noundofile

I was going to suggest that you decide, before writing the undo file, how big it is going to be, then decide whether to set 'undofile'. It is probably easier to let vim write the file, then use a BufWritePost autocommand to check the size of the newly written file and, if it is too big, delete it.


The LargeFile - Edit large files quickly plugin detects large files and tweaks many options to accommodate this better. It also turns 'undolevels' off to avoid that large increase in memory you've seen.

  • Really ought to have been using this anyway--thanks for the suggestion. I'm accepting the other answer just because it's more specific to the particular problem I had, but +1 to yours as well. – Kyle Strand Apr 17 '14 at 18:05

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.