76

Where does VIM (gvim/macvim) keep swap files for unsaved/unnamed buffers? (If it does so at all).

Background: Throughout a semi intense seminar I was taking notes in an unnamed/unsaved MacVim buffer when the MacBook ran out of juice and powered down out of nowhere (It did not sleep/hibernate as it usually would).

Question: Would anyone know if there is any chance that the unsaved work may have been saved to a swap (.swp) file or the like, that could be recovered?

System details: In particular this happened using MacVim on a Mac OS X 10.5.8 (But possible recovery hints for other versions are of interest too). I have not restarted MacVim yet in case doing so will initiate a cleanup process.

davur
  • 917
  • 1
  • 9
  • 13
  • 2
    Clarification: By unsaved I mean never saved. File was created by pressing Command-T (I think equivalent to :tabe), i.e. a new unnamed buffer is opened in a new tab. (I would usually do ':sav somefilename' at a later stage. – davur Oct 05 '10 at 01:15
  • Doesn't matter. See the additions to my answer. – frabjous Oct 05 '10 at 01:28

5 Answers5

88

Start up vim and try:

:recover <filename>

If the file never had a name, then simply:

:recover

That's your best bet. For more about swap files and recovery, see:

:help usr_11

About the swap files, typically they're saved in the same directory as the file being edited, but with a . added the beginning to make it hidden and .swp at the end, but it's possible to move them elsewhere by something like:

:set directory=~/vimswap

or similar.

See:

:help swap

For all the details.

A vim swap file is not the same as the edited buffer, however, so be sure to read up there on what can be done for recovery.


EDIT: comments answering the question:

[…] It seems to look in your current working directory, ~/tmp, /var/tmp and /tmp for swap files and in my case I always have a current working directory set and that's where it got saved. – dsclementsen Oct 5 '10 at 1:42

also, be sure to check out the vim -r command line arg. This will print out all the swap files found and where they are. In addtion it will have a lot of extra information such as date/modified/username/etc... – Neg_EV Oct 5 '10 at 13:49

Aaron Thoma
  • 690
  • 8
  • 16
frabjous
  • 10,755
  • 3
  • 34
  • 27
  • This is not going to help. By unsaved, I mean never saved. Meaning I opened a new buffer with :tabe or (Command+T). So we're talking about an unsaved/unnamed buffer. Is there a default location where this sort of unsaved work is stored? – davur Oct 05 '10 at 01:13
  • 4
    Try `:recover` with no file name. I just tested that for recovering an unnamed file and it worked. Anyway, even if that particular command doesn't help, reading those help files might. – frabjous Oct 05 '10 at 01:24
  • I am just worried about opening vim at all, in case it somehow knows that it crashed last time and does some cleanup that I don't want it to do. – davur Oct 05 '10 at 01:30
  • 1
    It doesn't as far as I can tell. I just tested by killing vim externally while editing a new file. But when I ran `recover` with no argument, it let me choose even from some older sessions. Anyway, the .swp file for an unnmaed buffer is probably in /tmp, but it would be somewhere else depending on whether you had `set directory= `. – frabjous Oct 05 '10 at 01:35
  • :recover with no file name worked! I had two empty buffers open and they were saved as .swp and .swo in my current working directory. – davur Oct 05 '10 at 01:39
  • Thanks frabjous. It seems to look in your current working directory, ~/tmp, /var/tmp and /tmp for swap files and in my case I always have a current working directory set and that's where it got saved. – davur Oct 05 '10 at 01:42
  • Glad it worked. – frabjous Oct 05 '10 at 01:57
  • 3
    also, be sure to check out the vim -r command line arg. This will print out all the swap files found and where they are. In addtion it will have a lot of extra information such as date/modified/username/etc... – Neg_EV Oct 05 '10 at 13:49
  • 1
    I had to use the command `:recover!` to recover my unsaved file. –  Nov 01 '13 at 19:56
  • `:recover` appears to only list swap files matching `*.sw*`. However, if none of those are available when creating a new swap file, vim will choose other names starting with `.s`, e.g. `.svz` But recover *does* work for those if you specify the filename, so if you find the complete list using `find -name '.s*'` or `ls .s*`, you can then open the specific file via, e.g., `:recover .svz`. – Ray Jul 13 '18 at 05:17
  • 13 years later this my life. Vim is awesome – mrbolichi Apr 29 '23 at 16:45
6

I work on Windows 10 and :recover didn't find a swap file. vim -r listed the swap file from the last edit session (also never saved) named _.swp. Recovering was possible with :recover _.

rolacher
  • 61
  • 1
  • 2
1

The answer is: all over the place. The trick is finding the right one. Some are in your /tmp directory, but lots of them are somewhere under your home directory.

There maybe more efficient ways to do this, but the following worked for me when I lost an unnamed file (on MacBook):

in home directory, search for backup files (takes a few seconds, and can probably be made smarter):

find ./ -name ".s*" > findVimBackups.txt

open file:

vim findVimBackups.txt

remove file names that aren't for backups of unnamed files:

:g!/\/\.s..$/d
:g/svn/d

Now I see a list of the locations of unnamed backup files. In each of those directories I run the following until I find the file(s) from today:

ls -ltra <directory>

I cd into the right directory and open vim and type :recover and select the correct backup.

TimHelck
  • 11
  • 2
0

It seems that they will end up in your working directory :pwd This is wherever you opened vim or alternately you might have set it using :cd or similar.

swap files for unnamed buffers seem to just get dumped in there with no filename so you'll end up with .swp .swn .swo etc. I discovered this not when trying to recover something but when my hg ignore file wasn't covering enough different suffixes!

I guessed having lots of unnamed buffers with changes might have been the cause and deleting these buffers caused the mystery files to disappear

JonnyRaa
  • 213
  • 1
  • 3
  • 10
0

I was able to recover my file on a win 10 box as follows:

Go the directory you last opened vim. For me, that was via the vim short cut on the win 10 task bar. Right click on the vim icon, right click again on the vim short cut and select 'properties'. A dialog will pop up. The value in the 'Start in:' field on that dialog is where I effectively last opened vim. For me, that value was %HOMEDRIVE%%HOMEPATH%. Goto a dos command console and invoke the commands echo %HOMEDRIVE% and echo %HOMEPATH% to determine where that is.

Examine the last modified dates of all the _.s* files in that directory. Find the file with the last modified date that matches when you last opened the file.

Open vim. Type :recover _filename_ where filename is the file you found in the prior instruction.

DarkDiamond
  • 1,875
  • 11
  • 12
  • 19
JohnC
  • 101
  • 2