vim: recover an unnamed file

1,115 views
Skip to first unread message

ping

unread,
May 16, 2013, 6:53:51 PM5/16/13
to vim_use
guys:
My PC (ubuntu) just restarted by itself due to the (stupid) overheat
issue, again (I couldn't find a solution on that in a year..)

but regardless, I had a file that I had been editing for quite a while.
all of a sudden the PC reloaded, now I'm wondering how can I recover
that file.

I tried both of the following method , but none of them give me the
right file.

one thing I've notice that if I'm in different folder, vim -r give me
different files. I tried to change to different folder and invoke vim
-r, but still failed to find the right one (per the timestamp) for me...

any other good ideals?

maybe the last resort, do I have to grep from the whole harddisk for this?

and, what's the best practice to solve this issue in the future (lesson
learned for me: always use a named buffer ) ?


1) :recover

Swap files found:
Using specified name:
1. .swa
owned by: ping dated: Fri Jan 21 17:26:35 2011
file name: [No Name]
modified: YES
user name: ping host name: 640g-laptop
process ID: 14418
2. .swb
owned by: ping dated: Wed Dec 29 14:32:22 2010
file name: [No Name]
modified: no
user name: ping host name: 640g-laptop
process ID: 7874
3. .swc
owned by: ping dated: Fri Dec 17 17:05:02 2010
file name: [No Name]
modified: YES
user name: ping host name: 640g-laptop
process ID: 2735
4. .swd
owned by: ping dated: Mon Dec 6 17:57:06 2010
file name: [No Name]
modified: YES
user name: ping host name: 640g-laptop
process ID: 22947
5. .swe
owned by: ping dated: Fri Oct 29 08:15:23 2010
file name: [No Name]
modified: YES
user name: ping host name: 640g-laptop
process ID: 20799
6. .swf
owned by: ping dated: Thu Dec 2 13:05:32 2010
file name: [No Name]
modified: YES
user name: ping host name: 640g-laptop
process ID: 5370
7. .swg
owned by: ping dated: Sat Oct 23 11:38:18 2010
file name: [No Name]
modified: YES
user name: ping host name: 640g-laptop
process ID: 4930
8. .swh
owned by: ping dated: Sat Oct 23 06:40:55 2010
file name: [No Name]
modified: YES
user name: ping host name: 640g-laptop
process ID: 3119
9. .swi
owned by: ping dated: Sat Oct 16 23:28:22 2010
file name: [No Name]
modified: YES
user name: ping host name: 640g-laptop
process ID: 13572
10. .swj
owned by: ping dated: Sat Oct 23 06:35:33 2010
file name: [No Name]
modified: YES
user name: ping host name: 640g-laptop
process ID: 4930
11. .swk
owned by: ping dated: Tue Oct 12 10:15:05 2010
file name: [No Name]
modified: YES
user name: ping host name: 640g-laptop
process ID: 13672
12. .swl
owned by: ping dated: Sun Oct 10 06:44:29 2010
file name: [No Name]
modified: YES
user name: ping host name: 640g-laptop
process ID: 26777
13. .swm
owned by: ping dated: Wed Oct 6 00:44:37 2010
file name: [No Name]
modified: YES
user name: ping host name: 640g-laptop
process ID: 29373
14. .swn
owned by: ping dated: Tue Oct 5 03:59:02 2010
file name: [No Name]
modified: YES
user name: ping host name: 640g-laptop
process ID: 29373
15. .swo
owned by: ping dated: Sun Sep 26 09:34:44 2010
file name: [No Name]
modified: YES
user name: ping host name: 640g-laptop
process ID: 3824
16. .swp
owned by: ping dated: Fri Apr 20 15:59:12 2012
file name: [No Name]
modified: YES
user name: ping host name: 640g-laptop
process ID: 17594
In directory ~/tmp:
-- none --
In directory /var/tmp:
-- none --
In directory /tmp:
-- none --

Enter number of swap file to use (0 to quit): 0



2) ping@640g-laptop:~$ vim -r


Swap files found:
In current directory:
1. .%.swp


owned by: ping dated: Mon Jan 9 14:45:26 2012


file name: ~ping/%


modified: YES


user name: ping host name: 640g-laptop


process ID: 27934


2. .Narrow_Region_3_VOOM19.swp


owned by: ping dated: Thu Jan 3 08:18:35 2013


file name: ~ping/Narrow_Region_3_VOOM19


modified: YES


user name: ping host name: 640g-laptop


process ID: 13556


3. .RZ2-l2vpn-lsonly-3.ol.swp


owned by: ping dated: Thu Sep 15 18:37:41 2011


file name: ~ping/RZ2-l2vpn-lsonly-3.ol


modified: no


user name: ping host name: 640g-laptop


process ID: 27006


4. .a.scr.swo


owned by: ping dated: Tue Jun 14 11:03:25 2011


file name: ~ping/a.scr


modified: no


user name: ping host name: 640g-laptop


process ID: 4253


5. .au-lsof.html.swo


owned by: ping dated: Tue Jan 10 19:01:20 2012


file name: ~ping/au-lsof.html


modified: YES


user name: ping host name: 640g-laptop


process ID: 9142


6. .mailcap.swp


owned by: ping dated: Thu Dec 29 14:04:17 2011


file name: ~ping/.mailcap


modified: no


user name: ping host name: 640g-laptop


process ID: 24540


7. .muttrc-gmail-gogetitok.swo


owned by: ping dated: Tue Jan 10 19:01:20 2012


file name: ~ping/.muttrc-gmail-gogetitok


modified: no
user name: ping host name: 640g-laptop
process ID: 9142
8. .muttrc-gmail.swo
owned by: ping dated: Tue Jan 10 19:01:20 2012
file name: ~ping/.muttrc-gmail
modified: no
user name: ping host name: 640g-laptop
process ID: 9142
9. .muttrc-jnpr.ori.swp
owned by: ping dated: Tue Feb 19 11:31:19 2013
file name: ~ping/.muttrc-jnpr.ori
modified: no
user name: ping host name: 640g-laptop
process ID: 20434
......








Rostislav Svoboda

unread,
May 16, 2013, 7:10:25 PM5/16/13
to vim...@googlegroups.com
> My PC (ubuntu) just restarted by itself due to the (stupid) overheat issue,
> again (I couldn't find a solution on that in a year..)

http://askubuntu.com/questions/142688/cpu-frequency-scaling-for-12-04

I have a couple of stories about a friend of mine who drilled a hole
to his laptop trying to improve cooling, to no avail. He ended up
using ice-packs underneath his laptop.

Bost

PS: Oh sorry I'm on a wrong mailing list :)

tooth pik

unread,
May 16, 2013, 7:28:19 PM5/16/13
to vim...@googlegroups.com
On Thu, May 16, 2013 at 06:53:51PM -0400, ping wrote:
> guys:
> My PC (ubuntu) just restarted by itself due to the (stupid) overheat
> issue, again (I couldn't find a solution on that in a year..)

> but regardless, I had a file that I had been editing for quite a while.
> all of a sudden the PC reloaded, now I'm wondering how can I recover
> that file.

one thing I've found helpful in different circumstances is to set the
'directory' option in my .vimrc to a single known folder (~/.vim-tmp)
-- that way all swap files are created in the same place, facilitating
both recovery and scripting -- I like to make sure there are no
swapfiles before starting an extensive session

sc

Gary Johnson

unread,
May 16, 2013, 8:48:25 PM5/16/13
to vim_use
After a quick look at Vim's memline.c, I think you are the victim of
poor housekeeping and a bug in Vim. You may still be able to
recover your files though.

Note that your oldest swap file is named ".swp", the next oldest is
named ".swo", and that pattern of decreasing last letters continues
through your newest swap file which is named ".swa". Note, too,
that your newest swap file is pretty old, from January 2011. I
doubt that that is the last time you used an unnamed buffer.

When Vim creates a swap file, it first tries the suffix ".swp". If
that file exists, Vim decrements the last letter and tries again.
When the suffix becomes ".swa" and that file exists, Vim then
decrements the second-to-last letter, sets the last letter to "z",
and tries again. It continues to look for swap file names that
don't exists until it reaches the suffix ".saa", then gives up.

Now, when Vim looks for swap files, it looks for files with suffixes
matching the pattern ".sw?". This will find only the first 16 swap
files. It ignores any swap files created with a second letter other
than "w".

That is a bug and needs to be fixed. Vim should either look for
swap files whose suffixes match the pattern ".s??" or stop creating
swap files with suffixes whose second letter is other than "w".

To your problem: It is quite likely that the same directories that
contain files named ".swa" also contain files named ".svz", ".svy",
and so on. Those are valid Vim swap files, but Vim doesn't
recognize them as such by those names.

I would suggest first verifying that you do not care about the files
matching the pattern ".sw?", then delete them. Then find the newest
file with a name matching the pattern ".s??", rename it to ".swp",
and try recovering it.

Once you've recovered all your files, I would suggest that you close
all instances of Vim and do a sweep of your file system using find
(not grep) to find all your swap files. Again be sure that you do
not need them, then delete them all. In the future, pay attention
to swap files left over after crashes and keep them cleaned up.

Regards,
Gary

ping

unread,
May 16, 2013, 10:23:37 PM5/16/13
to vim...@googlegroups.com, Rostislav Svoboda
yes I think this is a really stupid issue in ubuntu that is lacking a
good solution now.
I'm wondering I maybe need to re-set the temporation limit in bios...

ping

unread,
May 16, 2013, 10:25:13 PM5/16/13
to vim...@googlegroups.com, tooth pik
On 5/16/2013 7:28 PM, tooth pik wrote:
> one thing I've found helpful in different circumstances is to set the
> 'directory' option in my .vimrc to a single known folder (~/.vim-tmp)
> -- that way all swap files are created in the same place, facilitating
> both recovery and scripting -- I like to make sure there are no
> swapfiles before starting an extensive session
that sounds a good idea, but what will happen if you have two files in
different folders have the same name?
will VIM associate each file with a different swap file name without
confusion?

thanks!

regards
ping

ping

unread,
May 16, 2013, 10:41:33 PM5/16/13
to vim_use
thanks Gary, I think I only undertsand some part of your explanations.
can you detail the steps for me to recover my file?


Ping

unread,
May 17, 2013, 12:09:11 AM5/17/13
to ping, vim_use
and, I just found that I had the swap file disabled :(

Gary Johnson

unread,
May 17, 2013, 1:36:27 AM5/17/13
to vim_use
On 2013-05-17, Ping wrote:

> On May 16, 2013, at 10:41 PM, ping wrote:
>
> > On 5/16/2013 8:48 PM, Gary Johnson wrote:

> >> After a quick look at Vim's memline.c, I think you are the victim of
> >> poor housekeeping and a bug in Vim. You may still be able to
> >> recover your files though.

[...]

> >> To your problem: It is quite likely that the same directories that
> >> contain files named ".swa" also contain files named ".svz", ".svy",
> >> and so on. Those are valid Vim swap files, but Vim doesn't
> >> recognize them as such by those names.
> >>
> >> I would suggest first verifying that you do not care about the files
> >> matching the pattern ".sw?", then delete them. Then find the newest
> >> file with a name matching the pattern ".s??", rename it to ".swp",
> >> and try recovering it.
> >>
> >> Once you've recovered all your files, I would suggest that you close
> >> all instances of Vim and do a sweep of your file system using find
> >> (not grep) to find all your swap files. Again be sure that you do
> >> not need them, then delete them all. In the future, pay attention
> >> to swap files left over after crashes and keep them cleaned up.

> > thanks Gary, I think I only undertsand some part of your explanations.
> > can you detail the steps for me to recover my file?

> and, I just found that I had the swap file disabled :(

That's unfortunate. Does that mean that you no longer need a better
explanation?

Regards,
Gary

Gary Johnson

unread,
May 17, 2013, 1:56:56 AM5/17/13
to vim...@googlegroups.com
You can specify 'directory' such that there are no name collisions.
See

:help 'directory'

There it explains that

- For Unix and Win32, if a directory ends in two path separators "//"
or "\\", the swap file name will be built from the complete path to
the file with all path separators substituted to percent '%' signs.
This will ensure file name uniqueness in the preserve directory.
On Win32, when a separating comma is following, you must use "//",
since "\\" will include the comma in the file name.

For example, this is how I set 'directory' in my ~/.vimrc:

if has("win32")
set directory=~/tmp/vimswap//,$TMP//,$TEMP//
elseif has("unix")
set directory=~/tmp/vimswap//,~/.vimswap//,~/tmp//,/var/tmp//,/tmp//
else
set directory-=.
endif

I don't know what OS I would ever use other than Windows or Unix,
but I don't want to ever create a swap file in any directory other
than one I've chosen for that purpose. Doing so would change the
modification time of the directory even though I may not have made
any other changes to it.

Regards,
Gary

ping

unread,
May 17, 2013, 7:40:36 AM5/17/13
to vim...@googlegroups.com
thanks Gary, that looks nice and self-adaptive
I found another one here:
https://github.com/docwhat/homedir-vim/blob/master/vimrc/.vimrc

it looks more complex, do you think it is good ?


ping

unread,
May 17, 2013, 7:56:26 AM5/17/13
to vim_use
I think for now there might be no way for me to get that back , since I
clearly had this line in my vimrc:

setlocal noswapfile "disable swapfile,

I think I intentionlly did it for some purpose before .
one thing is I don't want to generate all of those aux files in like a
system folder .

but after reading
[more](http://my.opera.com/peterchenadded/blog/2008/12/27/gvim-7-1-swap-and-backup-files)

I think I still should have the backup file turned on.

backupdir=.,~/tmp,~/

so I should still have my backup for unnamed files right?

now I learned this good way of putting in a centrollized folder
[here](https://github.com/docwhat/homedir-vim/blob/master/vimrc/.vimrc)

I just have a thought now, will the backup file remains when we delete
the original file? if so maybe we can still recover the whatever files
back even if the original ones (even an unnamed buffer? ) got deleted?


Gary Johnson

unread,
May 17, 2013, 10:32:12 AM5/17/13
to vim...@googlegroups.com
That approach looks fine to me.

Regards,
Gary

Gary Johnson

unread,
May 17, 2013, 10:55:32 AM5/17/13
to vim_use
For your unnamed buffers, there were no files, so there was nothing
to back up.

Regards,
Gary

Reply all
Reply to author
Forward
0 new messages