Hacker News new | ask | show | jobs
by justinmk 66 days ago
Vim undofile "data loss" happens because Vim (VIM!) will reset the undofile will reset the undofile if an external tool (git, nano) changed the file while Vim is not running.

Go ahead, try it:

    # Make some edits, then exit vim (remember to exit).
    vim --clean +'set undofile' foo.txt

    # Edit the same file with non-vim; save the changes.
    nano foo.txt

    # Open the file again and try "u". Then post on bluesky about Vim's "duty of care".
    vim --clean +'set undofile' foo.txt

This is documented at `:help undo-persistence`.
3 comments

Spot on. It genuinely surprises me how many people don't realize that vim's persistent undo feature resets if the file is modified by anything outside of vim itself.

For example, a common scenario is rolling back a file via git, which triggers this reset. However, considering they treated it as a reliable version control system without actually reading the documentation, they probably aren't using git anyway.

The good news for them, however, is that they actually can bypass this limitation and treat their undo history like version control—by using nvim-fundo [1]. The catch is that they'll have to switch to nvim, as there isn't a direct equivalent for vim.

1. [nvim-fundo](https://github.com/kevinhwang91/nvim-fundo)

it seems they are using 'autoread' (but I guess this only works if vim is running while the modification happens)

https://infosec.exchange/@david_chisnall/117172481079439378

https://www.vim.org/scripts/script.php?script_id=5206

And vim will also reset it while "recover"ing the last unsaved changes. I believe no user relies on it to actually persist the history of a file. If someone is doing that whether in nvim or vim I'd seriously advise you to use a real VCS repo instead.