Recover a deleted file from its swap file

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Recover a deleted file from its swap file

Nazri Ramliy
I have a vim swap file for a file that I have just deleted.

It is possible to get vim to (sort of) recover the content of the file
referred to by the swap file?

Thanks in advance for any help.

nazri.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Recover a deleted file from its swap file

meino.cramer
Nazri Ramliy <[hidden email]> [10-05-07 05:32]:

> I have a vim swap file for a file that I have just deleted.
>
> It is possible to get vim to (sort of) recover the content of the file
> referred to by the swap file?
>
> Thanks in advance for any help.
>
> nazri.
>
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>

I would guess: Yes.

First backup the swp-file (just to have more than one try)
then create with another editor an empty file with the name
of the deleted one.
Then start vim with that file and choose "recover".

HTH - Good luck!
best regards
mcc

--
Please don't send me any Word- or Powerpoint-Attachments
unless it's absolutely neccessary. - Send simply Text.
See http://www.gnu.org/philosophy/no-word-attachments.html
In a world without fences and walls nobody needs gates and windows.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Recover a deleted file from its swap file

Jeffrey 'jf' Lim
In reply to this post by Nazri Ramliy
On Fri, May 7, 2010 at 11:29 AM, Nazri Ramliy <[hidden email]> wrote:
> I have a vim swap file for a file that I have just deleted.
>
> It is possible to get vim to (sort of) recover the content of the file
> referred to by the swap file?
>
> Thanks in advance for any help.
>
>

vim "filename", and choose the recover option. See how that works out for you.

-jf

--
"Every nonfree program has a lord, a master --
and if you use the program, he is your master."
    --Richard Stallman

"It's so hard to write a graphics driver that open-sourcing it would not help."
    -- Andrew Fear, Software Product Manager, NVIDIA Corporation
http://kerneltrap.org/node/7228

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Recover a deleted file from its swap file

Tony Mechelynck
In reply to this post by Nazri Ramliy
On 07/05/10 05:29, Nazri Ramliy wrote:
> I have a vim swap file for a file that I have just deleted.
>
> It is possible to get vim to (sort of) recover the content of the file
> referred to by the swap file?
>
> Thanks in advance for any help.
>
> nazri.
>

Not sure:
- Does the swap file still exist? (usually .filename.ext.swp and by
default in the same directory as where the file used to be, but many
people put that elsewhere, or set 'noswapfile'... only to find when it's
too late that they lack esential information.
- Did you use :preserve before deleting the file?

See
        :help :preserve
        :help :recover
        :help ATTENTION
        :help -r
        :help crash-recovery

If you close Vim, it will delete the swapfile. Of course if you kill it
without letting it react ("kill -9" on Unix-like sstems) it won't...
unless it's already gone.


Best regards,
Tony.
--
Impossible, adj.:
        (1) I wouldn't like it and when it happens I won't approve;
(2) I can't be bothered; (3) God can't be bothered.  Meaning (3) may
perhaps be valid but the others are 101% whaledreck.
                -- Chad C. Mulligan, "The Hipcrime Vocab"


--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Recover a deleted file from its swap file

Nazri Ramliy
Thank you all for your enlightening responses.

My problem is gone (it's was a PEBKAC).

Here's what happened:

I edited a file, typed something in, then decided that the file is
better saved in a different directory so I moved the file to that
other directory. I typed more stuff into the file and save it. I don't
remember the exact steps I took to do the file move.

Move forward a few hours later, I open the file in its new location
and I only see the first content that I typed in before, not the
latest! So I thought I must have made this mistake:

<thinking>
1. Edited a file, saved (without quitting vim).
2. In another terminal, *copied* the file to its new location.
3. Edited the file (in the same vim session), added more content, saved.
4. In that other terminal, I deleted the 'old' file.
</thinking>

When I opened the file in its new location I see the old content.

At this point my thought was *!#*!#&!*@W!Y&#*!&.

Then I remember that vim have a swap file and in that old directory
there's exactly one swap file left over from some previous vim editing
session.

I created a new file with the same name as the file that the swap file
is associated with but when I opened it in vim vim just created a new
swapfile, 'ignoring' the old one.

That's when I decided to ask the list.

My attempts at using the :recover command failed (after creating a
file with the same name as what the swap file is associated with) with
a message saying that the swap file is broken.

That was last Friday.

Fast forward to today (Monday):

Frustrated by this I decided enough is enough. I gotta handle it like
a man: Don't fret over the lost bytes. So I proceeded with deleting
the stale swap file.

Then I go to the new directory where the file was moved to and opened
the file and vim happily told me that a swap file exist for that file
and when I choose recover *blam* the latest content of that file that
I thought is gone forever is there, every single character!

Heheh.. if that is not a case of PEBKAC then I don't know what is :)

nazri.

P/S: If you are curious the file contains usernames and passwords for
"fake" gmail accounts to test out the web app in my $day_job.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Recover a deleted file from its swap file

Tony Mechelynck
On 10/05/10 03:39, Nazri Ramliy wrote:

> Thank you all for your enlightening responses.
>
> My problem is gone (it's was a PEBKAC).
>
> Here's what happened:
>
> I edited a file, typed something in, then decided that the file is
> better saved in a different directory so I moved the file to that
> other directory. I typed more stuff into the file and save it. I don't
> remember the exact steps I took to do the file move.
>
> Move forward a few hours later, I open the file in its new location
> and I only see the first content that I typed in before, not the
> latest! So I thought I must have made this mistake:
>
> <thinking>
> 1. Edited a file, saved (without quitting vim).
> 2. In another terminal, *copied* the file to its new location.
> 3. Edited the file (in the same vim session), added more content, saved.
> 4. In that other terminal, I deleted the 'old' file.
> </thinking>
>
> When I opened the file in its new location I see the old content.
>
> At this point my thought was *!#*!#&!*@W!Y&#*!&.
>
> Then I remember that vim have a swap file and in that old directory
> there's exactly one swap file left over from some previous vim editing
> session.
>
> I created a new file with the same name as the file that the swap file
> is associated with but when I opened it in vim vim just created a new
> swapfile, 'ignoring' the old one.
>
> That's when I decided to ask the list.
>
> My attempts at using the :recover command failed (after creating a
> file with the same name as what the swap file is associated with) with
> a message saying that the swap file is broken.
>
> That was last Friday.
>
> Fast forward to today (Monday):
>
> Frustrated by this I decided enough is enough. I gotta handle it like
> a man: Don't fret over the lost bytes. So I proceeded with deleting
> the stale swap file.
>
> Then I go to the new directory where the file was moved to and opened
> the file and vim happily told me that a swap file exist for that file
> and when I choose recover *blam* the latest content of that file that
> I thought is gone forever is there, every single character!
>
> Heheh.. if that is not a case of PEBKAC then I don't know what is :)
>
> nazri.
>
> P/S: If you are curious the file contains usernames and passwords for
> "fake" gmail accounts to test out the web app in my $day_job.
>

Well, that's an argument against acting on a single file from two
different programs at the same time if I ever saw one.

Instead of copying the file to the new location in another terminal, use

        :saveas /new/directory/filename.ext

This creates a copy of the current file at the new location and starts
editing the new file. If you want to immediately delete the old file,
you can do

        :if has('unix') | exe '!rm' expand('#') | else | exe '!del' expand('#')
| endif

( ;-) ) all on one line, and you will usually know whether or not you're
on Windows, so you can instead type either

        :!rm #
or
        :!del #

which will remove the alternate file (the file which you edited just
before the current file). To remove it later, use :!rm or :!del
(depending on platform) followed by enough of a pathfilename to avoid
removing something else.


Best regards,
Tony.
--
All of the true things I am about to tell you are shameless lies.
                -- The Book of Bokonon / Kurt Vonnegut Jr.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Recover a deleted file from its swap file

Christian Brabandt
Hi Tony!

On Mo, 10 Mai 2010, Tony Mechelynck wrote:

> ( ;-) ) all on one line, and you will usually know whether or not you're  
> on Windows, so you can instead type either
>
> :!rm #
> or
> :!del #
>
> which will remove the alternate file (the file which you edited just  
> before the current file). To remove it later, use :!rm or :!del  
> (depending on platform) followed by enough of a pathfilename to avoid  
> removing something else.

or with any vim, that has +eval support:

:call delete(expand('#'))

regards,
Christian

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php