This goes into recovery mode and gives you more info. I think this is what you want.
.swX -files are left behind if a session with an unsaved file is killed/crashes/something else bad happens. They are also present during the time an unsaved buffer is open in a Vim session. vim -r reads these temporary files and recreates the content. After you've recovered it, just save it as usual, e.g. :w newfilename .
answered Oct 30, 2010 at 10:33 Daniel Andersson Daniel Andersson 24.6k 5 5 gold badges 59 59 silver badges 61 61 bronze badgesPrecisely what I was looking for. For anyone else, this is how you recover as-of-yet unsaved files. The above methods work fine if a file already exists, but you can't open a file with no name.
Commented Jan 18, 2012 at 23:22Note that here .swp should be replaced with the swap file name. If your directory option ends with // , then the file will be named %path%to%.swp in the provided directory instead because of a Vim feature: " - For Unix and Win32, if a directory ends in two path separators "//", the swap file name will be built from the complete path to the file with all path separators replaced by percent '%' signs (including the colon following the drive letter on Win32). This will ensure file name uniqueness in the preserve directory. "
Commented Jun 9 at 7:11You don't need to have the original file to recover the .swp. Just open the file as if it exists. vim will look for the file with the .swp extension and offer to recover it.
$ ls -a . .. .test.txt.swp $ vim test.txt [. ] Swap file ".test.txt.swp" already exists! [O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:
Just press R to recover it and :wq the file
Edit: Note that the .swp file only contains the changes done to the file (see comment). This means that you will need to fetch a recent copy of the file from backup and then use vim to recover the latest changes. If you don't have a backup copy of the file you're really out of luck.
103 3 3 bronze badges answered Oct 28, 2010 at 7:41 Vegar Westerlund Vegar Westerlund 363 1 1 silver badge 6 6 bronze badgesYou definitely have to have the original file, without it the data in the .swp file is useless. See ":help swap-file". There are situations where it may appear to work without the original file, but the .swp file only stores change information and requires some starting point to apply that information during recovery.
Commented Oct 28, 2010 at 17:57You are right, of course. I actually didn't know this about vim. But it is only logical as storing a complete copy of the original file would be a huge waste. Then the answer is: Recover your file from backup, then use vim to recover the latest modifications.
Commented Nov 2, 2010 at 9:27Storing the entire file in a single swp would be wasteful, yes, but I see potential value in accumulating swp files (when enabled.) For the same reason you edit via diff and patch the original only on explicit save, it makes sense to me if you could stage a sequence of diffs, generate a partial sum (dates before today -> nightly; nightly + today = current) etc. I found ':pre[serve]` in the manuel, I think it works that way. In any case, the most important line is: The original file is no longer needed for recovery.
Commented Aug 1, 2017 at 15:15Looks like it works - :preserve and :set cpoptions=. & leaves a .swp and .swo file in the directory and offers to recover from either. Using vim -r
(Continued) - the buffer carries the flags and it's the buffer that produces the swap files, so even if the flags aren't set from global configuration or autoload, they are inherited during recovery itself. To replace a recovery sequence with a single recovery file, you can move the swap you want to .swp and hide the rest. I'd love to know more about the overlap between file recovery and Vim session management if anyone can fill in the gaps.
Commented Aug 1, 2017 at 16:091 Open the swap file .file.swp in vim
2 :recover will recover your file
answered Nov 8, 2014 at 6:20 311 2 2 silver badges 6 6 bronze badgesAfter reading Vegar Westerlund and Heptite comments, I wanted to know at what point vim needs the original file (or a backup) to recover from a .swp file. Here's what did:
I opened a 975 lines file, edited it on line 949 (creating a .swp file) and killed the process, then deleted the original. $ vim Original_File asked if I wanted to recover from the .swp » > yes; only the first 68 lines and the last 34 lines (starting 8 lines above my edit) were actually recovered.
I then repeated the test with smaller files: From 20 up to 200 lines, the .swp file contained 100% of the original content. But at 300 lines, only the first 68 and the 18 last lines (starting 2 lines above my edit) were included in the .swp.
As a conclusion it's good to know that vim always save the file's "header" and "bottom" in the .swp files. Maybe there's a setting to control how much lines should a .swp contains?