Vim persistent undo retains orphaned undo files indefinitely
0 reputation · 18 Nov 2024, 14:15 UTC
0 reputation · 18 Nov 2024, 14:15 UTC
When 'undofile' is enabled, Vim stores session undo history in hash-named files under 'undodir'. The files persist after Vim exits and are tied to the full path of the edited buffer, so renaming, moving, or deleting a source file can leave its undo file unreachable. Vim documents no built-in command to list, age out, or remove these stale files, and undo files are not guaranteed to remain compatible across major Vim versions or with Neovim.
The goal is to define a retention boundary for 'undodir' without losing usable undo history for active files. Relevant constraints include filesystem permissions, sensitive text written to disk, and the absence of version-migration behavior for undo files.
Should Vim expire undo files when their source path no longer exists, after a configurable age, or when 'undodir' exceeds a size threshold? What metadata, if any, should Vim record to distinguish active undo files from orphans?
37775 reputation · 18 Nov 2024, 15:05 UTC
Vim does not expire orphaned undo files, and it should not be expected to do so. With 'undofile' enabled, Vim writes a hash-named file to 'undodir' based on the edited buffer's full path. Vim has no garbage collector, no reverse lookup from undo file to source path, and no recorded creation time or source-path metadata. It therefore cannot delete an undo file when its source disappears, nor can it enforce an age or size limit from inside the editor.
Undo files are keyed by a hash of the full buffer path. Once Vim exits, it no longer tracks which source path produced which hash-named file, so a renamed, moved, or deleted source file leaves its undo file unreachable but still present.
undodir size.Impose retention outside Vim. Use one of these scoped approaches:
undodir. Set undodir to a directory inside each project tree, such as .vim/undo, and remove it with the project. This bounds orphan accumulation by project lifetime rather than file age.undodir exceeds a limit. This does not distinguish active from orphaned files.:set undofile? undodir? in Vim to confirm persistent undo is enabled and identify the directory.undodir.undodir first. Compare undo-file mtimes against recent edits and verify open buffers are unaffected.undodir permissions to match the confidentiality of edited files because undo files can contain sensitive buffer text.undodir across Vim and Neovim or across major Vim versions without testing compatibility; no migration path is provided.One diagnostic detail that could change this recommendation: do you need to share undodir between Vim and Neovim, or across multiple Vim major versions? If yes, avoid cross-version undo reuse entirely and use separate directories per editor version.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.