I have undodir=~/.vim/undodir such that persistent-undo history files don't clutter my source code directories.

However, for one particular source file, I'd like to have its persistent-undo history file to be stored in the same directory as the source file itself — such that I could move them together to a different machine and the undo history would portably be still there on the other machine.

It seemed natural to use a modeline for this, so I tried:

# vi: undodir=.:undofile

But when opening the file, I got:

Error detected while processing modelines:
line 1:
E520: Not allowed in a modeline: undodir=.

I guess this is because 'undodir' is a global setting...

Now I'm not sure if there is a workable way to do this...

1 Answer 1


No, the 'undodir' option cannot be set from a modeline, because it has the 'SECURE' flag set.

Since this is a global option, it does not make sense to set this option differently for a specific file, that would also affect all other buffers that are edited later then.

However it might be possible to use a BufRead in combination with a BufWritePost autocommand to do what you want.. Something similar to this:

set undodir=~/tmp/undodir,. undofile
augroup MyUndoDir
  au BufRead /tmp/foobar.vim :setl noundofile
  au BufWritePost /tmp/foobar.vim  exe "wundo ". fnamemodify(@%, ':h'). '/.'. fnamemodify(@%, ':t'). '.un~'
augroup END

Since the 'undofile' option has been disabled for that buffer whenever we edit it, we need to explicitly write the undofile on an BufWritePost autocommand. Of course you need to use the actual filename instead of /tmp/foobar.vim in the example.

Your Answer

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

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