vimtricks.wiki Concise Vim tricks, one at a time.

How do I prepend a custom persistent undo directory while keeping existing undodir paths in Vim?

Answer

:set undodir^=$HOME/.vim/undo//

Explanation

If you already have an undodir configured by a distro config or plugin, replacing it outright can remove fallback paths you still want. The ^= modifier prepends a value instead of overwriting, so your preferred directory is tried first while existing entries remain available.

How it works

  • undodir is a comma-separated list of directories where undo files are stored
  • ^= prepends to an option's current value
  • $HOME/.vim/undo// uses the double-slash form so Vim can create unique nested paths per file
:set undodir^=$HOME/.vim/undo//

This is especially useful when moving between machines or containers where global config injects defaults you don't want to lose.

Example

Before:

:set undodir?
undodir=~/.cache/vim/undo

After running the command:

:set undodir?
undodir=$HOME/.vim/undo//,~/.cache/vim/undo

Now Vim attempts your explicit path first, then falls back to the original location if needed.

Tips

Pair this with :set undofile to actually persist undo across sessions. Use :echo &undodir to inspect the final resolved list after your config files load.

Next

How do I save only modified files across the argument list in Vim?