Tuesday, February 1, 2011

Re: Help, please

> I've occasionally wished for a "--wtf" option to vim that would
> effectively open both the unrecovered and recovered versions in a
> diffsplit allowing me to compare them. Currently I have to
>
> 1) open the file with "-r" to recover
> 2) write the file to a temp file
> 3) quit vim
> 4) delete the swapfile (remembering "/a" on Dos/Win32)
> 5) vimdiff the original and the temp file
> 6) make any fixes and save the file back out
> 7) delete the tempfile
>
> Some easy means to do steps 1-5,7 would be most welcome.
>
> -tim

I do it a bit more easily:

1) Open the file with -r to recover
2) :DiffOrig (:help :DiffOrig)
3) Fix it up however I want
4) :w
5) :!rm the-name-of-the-swapfile.swp

And actually, I use the shell script below whenever I have a system
crash (which is usually because I remove the battery at an inopportune
moment...). It finds Vim swap files in the current directory, opens each
file in Vim in turn, and then prompts me to delete the swapfile after I
exit Vim. So that pretty much cuts out steps 1 and 5. Makes life pretty
sweet.

I wonder if there is something like this on the Vim Tips wiki, or
whether it would be worth adding.

Ben.

#!/bin/bash
SWAPS=`find . -name '.sw?' -or -name '.*.sw?'`
N=1
while true
do
SWAP=`echo "$SWAPS" | tail -n +$N | head -n 1`
if [ "$SWAP" = "" ]
then
break
fi
REAL=`echo "$SWAP" | sed -E 's/(^|\/)\.([^/]*)\.sw.$/\1\2/'`
N=$(($N+1))
if [ "$SWAP" = "$REAL" ]
then
vim -r "$SWAP"
else
vim "$REAL"
fi
if [ -f "$SWAP" ]
then
read -e -p "Delete the swap file?" DELETE
if [ "$DELETE" = "y" -o "$DELETE" = "Y" ]
then
rm "$SWAP"
fi
fi
done

--
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

No comments: