‹ BackHN Continuity

Thread

On caring for user data: NeoVim caused Vim undo files to be deleted

384 points · 346 comments · jandeboevrie

  1. justinmk · · focus · HN ↗
    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`.
    1. ofseed · · focus · HN ↗
      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](<a href="https:&#x2F;&#x2F;github.com&#x2F;kevinhwang91&#x2F;nvim-fundo" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kevinhwang91&#x2F;nvim-fundo)

    2. endospore · · focus · HN ↗
      And vim will also reset it while &quot;recover&quot;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&#x27;d seriously advise you to use a real VCS repo instead.
    3. pas · · focus · HN ↗
      it seems they are using &#x27;autoread&#x27; (but I guess this only works if vim is running while the modification happens)

      <a href="https:&#x2F;&#x2F;infosec.exchange&#x2F;@david_chisnall&#x2F;117172481079439378" rel="nofollow">https:&#x2F;&#x2F;infosec.exchange&#x2F;@david_chisnall&#x2F;117172481079439378

      <a href="https:&#x2F;&#x2F;www.vim.org&#x2F;scripts&#x2F;script.php?script_id=5206" rel="nofollow">https:&#x2F;&#x2F;www.vim.org&#x2F;scripts&#x2F;script.php?script_id=5206

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.