1

I accidentally removed about 1 hour of my work, how to restore? Last week I worked with file.md on my current computer and didn't save it into disk but just put my OS into a sleep mode. Yesterday I edited file.md on another machine. Today I made my first machine woken up and synchronized the file (via Syncthing) on both machines but not in the buffer of nvim at my current computer. So nvim asked me to load changes from the file into my current buffer because I have:

set autoread
au FocusGained,BufEnter * checktime

I agreed to load it from disk but then stupidly enough I pressed undo (u) (several times or/and something more?) and saved the buffer into the disk. How to restore the file? This looks like I lost it, right? Because I have no swapfile (:swapname is nothing; nvim -r shows no swap files), undo history does not contain the loaded text (pressing u and Ctrl-r does not contain the change) (reasonable because I didn't edit it just loaded from a disk), Syncthing doesn't keep versions of this file, the git repository doesn't contain it (because I didn't commit). So it looks my setup failed to save me from my stupidity.

2 Answers 2

1

I would install undotree plugin and search in the undo history if there is a branch that contains a version that could help you.

With u and Ctrl r you navigate along one branch of modification but if you have made one edit doing so you have created another branch not allowing u and Ctrl r to access the version you want.

You can also navigate through the versions in time based way with g- and g+ and this will then give you access to all the versions.

The undotree presents you all the versions listed by time but giving you hint about the undo branches.

Maybe this answer could be of some help to you.

1

To restore press g- several times.

I found how to restore and restored it! (Before Vivian posted his answer actually). See :help usr_32.txt about undotree. All I had to do is to press g- several times which moved me to another branch where I could find my changes.

1
  • 1
    I'm happy to hear you managed to find back your work, we all know how frustrating it can be. Vim undo functionality is actually very good a that :-) Commented Dec 4, 2023 at 5:37

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.