oops looks like i was wrong -- the .swp files are left around. the following
should fix this:
find . -name '*.swp' | \
sed 's/\(.*\/\)\.\([^.]\+\(\.[^.]\+\)\?\)\.swp/\1\2/' | \
while read i
do
cp $i $i".old"
vim -r $i -e -c 'wq'
if [[ `diff $i $i".old" | wc -l` -eq 0 ]]
then
mv $i".old" $i
rm `dirname $i`/"."`basename $i`".swp"
fi
done
the added rm command just reconstructs the name of the swp file from the
name of the original using backticks to pull out the path and the filename.
This is now a big ugly hack which could probably be cleaned up quite a bit,
but ought to work anyway.
On Tue, Oct 6, 2009 at 8:02 PM, Chris Suter <[email protected]> wrote:
>
>
> On Tue, Oct 6, 2009 at 1:52 PM, Yang Zhang <[email protected]> wrote:
>
>>
>> Sometimes, when vim or the host OS crashes with many open files, .swp
>> files are left scattered throughout the system. Currently, I locate
>> all the .swp files and manually open the corresponding original files
>> in vim so that I'm prompted with the option to recover what's in the
>> .swp. 90% of the time, the recovered file is identical to the
>> original, and 5% of the time, the recovered file is empty. In both
>> these cases, I simply discard the .swp.
>>
>> Is there a way (or are there any tools) to automate this highly manual
>> recovery process? I.e., given a set of .swp files, for each one
>> compare the recovered file with the original and discard the swp if
>> it's useless (leaving in place the ones that could not be
>> automatically pruned out)?
>> --
>> Yang Zhang
>> http://www.mit.edu/~y_z/ <http://www.mit.edu/%7Ey_z/>
>>
>> >>
>>
> Assuming you have the standard set of GNU tools at your disposal, something
> like the following should work (i created a simple test case and it worked
> for me). The only tricky bits are the sed regex, which assumes all the files
> are of the form "filename" or "filename.ext" -- if the filenames have any
> wierdness about them, it might not work, but assuming you've got some
> normal-ish source tree with *.c files or something, this should do. From the
> base of the directory in which the concerned swp files exist, you should be
> able to copy and paste the following directly into the terminal. It's
> probably not a bad idea to make a full backup of the directory first.
>
>
> find . -name '*.swp' | \
> sed 's/\(.*\/\)\.\([^.]\+\(\.[^.]\+\)\?\)\.swp/\1\2/' | \
> while read i
> do
> cp $i $i".old"
> vim -r $i -e -c 'wq'
> if [[ `diff $i $i".old" | wc -l` -eq 0 ]]
> then
> mv $i".old" $i
> fi
> done
>
>
> Hopefully my indentations come through or that might be kind of hard to
> read. Let me explain basically what's going on:
> 1) "find" generates a list of the swap files under the current directory.
> 2) the output of find is piped into sed, which converts the paths to the
> .swp files into the base filenames to which they correspond
> 3) the output of sed is piped into the "while read i" command, which will
> read each line of input and place it in the shell variable $i within the
> loop structure
> 4) the loop is mostly straightforward -- a copy of each file is made with
> ".old" appended to the filename, then the file is opened by vim with some
> command line options that basically force the recovery, write the file and
> quit vim without ever showing the interface (or interrupting the script to
> ask you stuff). Say your file was called "foo.c". If there were unsaved
> modifications to foo.c, then you'll now have "foo.c" and "foo.c.old" sitting
> next to each other with "foo.c" containing whatever changes were reflected
> in the swp file, and "foo.c.old" being whatever "foo.c" was at the time of
> the previous save. What remains is to replace the "recovered" version with
> the "old" version, only if the number of lines output by $ diff foo.c
> foo.c.old is zero. If any of this is unclear, read up on bash conditionals,
> and have a look at
> $ man test
> $ man diff
> $ man wc
> as needed.
>
> The end result is that files which were unchanged but left a swp file
> sitting around will have their swp files deleted. Any files which were
> actually modified will now show those modifications, with the previously
> saved version available as "foo.c.old", etc. The swp files corresponding to
> these modified files will also still be around and can be deleted, if you
> like, with something like $ find . -name '*.swp' | xargs rm
>
> Hope this helps -- feel free to hit me up if anything is unclear. Thanks
> for the challenge! This was a fun problem to solve.
> --
> Christopher Suter
> www.grooveshark.com
>
--
Christopher Suter
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---