Recover a vim file from the .un~ file without the undo command
Asked Answered
G

5

23

How can I restore a vim file from the undo file without hitting undo?

I had a vim file that I saved while adding text. Then I ran a python command that emptied the file's contents, and I can see some of the words the file contained in the file's .un~ file. When I try to undo in the file, it says Already at latest change. I can't find the swap file in my swap files directory.

Guaco answered 5/9, 2013 at 8:7 Comment(4)
you cannot find the swp file, because you have exited the vim. what is the output of set bk? and set wb?Granada
Both commands produce no output.Guaco
the question mark ?, is a part of the command. I am trying to get your backup setting, to see if it is possible to restore the file from your vim-backup file.Granada
I used :%s/\v^@{3}./\r/g on my vim undo file to get some useful results. (Press Ctrl+@ to input the null character shown as ^@; on US layouts Ctrl+2 or Ctrl+Shift+2 should work)Deranged
M
15

You can't. The undo information is linked to Vim's last knowledge of the file's contents; when they don't correspond any more, Vim cannot re-apply the changes. This is documented at :help undo-persistence:

Vim will detect if an undo file is no longer synchronized with the file it was written for (with a hash of the file contents) and ignore it when the file was changed after the undo file was written, to prevent corruption.

Best you can do is try to manually salvage recognizable bits in the undo file, e.g. with a hex editor, or Vim's binary mode.

Margherita answered 5/9, 2013 at 10:37 Comment(1)
I managed to salvage some of it running strings on the undo fileDegrade
I
26

As the other answers have noted, you can't recover the whole file from vim's undo files, simply because vim seems to only keep diffs in the undo files, not the whole contents. (That makes a lot of sense, as it's space efficient.)

One thing you can try though, is to extract what's possible from your undo file:

$ strings <undo-file>

The output will not be pretty, but you could end up finding something that's valuable to you.

Intro answered 23/10, 2018 at 12:52 Comment(0)
M
15

You can't. The undo information is linked to Vim's last knowledge of the file's contents; when they don't correspond any more, Vim cannot re-apply the changes. This is documented at :help undo-persistence:

Vim will detect if an undo file is no longer synchronized with the file it was written for (with a hash of the file contents) and ignore it when the file was changed after the undo file was written, to prevent corruption.

Best you can do is try to manually salvage recognizable bits in the undo file, e.g. with a hex editor, or Vim's binary mode.

Margherita answered 5/9, 2013 at 10:37 Comment(1)
I managed to salvage some of it running strings on the undo fileDegrade
P
7

It is not exactly possible, as the undo file only contains the text that was changed in a single change. If you at some point reloaded the file, the undofile should contain the complete buffer for that and starting from there one could theorectically recover the file (by going through the undo states).

I have written about this before at the vim_use mailinglist here and here (which even contains a patch, that let's you force reading in the undo-file)

You could try to patch vim and see if you can recover at least some data.

Penitential answered 7/9, 2013 at 12:5 Comment(1)
Newer versions of the patch: 8.0 , 8.2Gloom
G
2

A reminder that if you have set in your .vimrc file

set backupdir=$HOME/tmp

You may have temp copies of the files that are readable and that can be renamed

Goy answered 26/7, 2018 at 12:53 Comment(0)
K
-1

The OP may still have had the vi(m) session open, as s/he tried to undo. I was surprised by a ':undo 2' which chopped the file. After a quick sudo apt-get install vim-runtime later (in a different shell!), :help undo led to to :help undo-branches, which lists the possible undo options. This may have helped the OP and further people with similar problems.

Kovar answered 3/6 at 11:59 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Chrysalid

© 2022 - 2024 — McMap. All rights reserved.