Recovering After Vim Terminates
When vim is shutdowns abnormally it leaves a bunch of .swp files around.1 Given the way I program (very iterative and test focused) those backups are rarely more current than the saved file. A great time saver to help with recovery is the vim
DiffOrig command. After learning about it on StackOverflow, I put together the following bash script to find any swap files under the current directory, open each in vim, run DiffOrig and prompt to delete after you close vim.
#!/bin/bash swap_files=`find . -name "*.swp"` for s in $swap_files ; do orig_file=`echo $s | perl -pe 's!/\.([^/]*).swp$!/$1!' ` echo "Editing $orig_file" sleep 1 vim -r $orig_file -c "DiffOrig" echo -n " Ok to delete swap file? [y/n] " read resp if [ "$resp" == "y" ] ; then echo " Deleting $s" rm $s fi done
I was planning on converting this to a vim function, but for now the extra step of exiting vim and answering a prompt works fine. Also, my vim already loads
DiffOrig. ArchLinux includes it in
/etc/vimrc. If your vim doesn't, I believe the following command will do it:
if !exists(":DiffOrig") command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis \ | wincmd p | diffthis endif
BTW Iron Men, I realize this post isn't really Perl focused, but it is usually Perl files that I'm recovering!
1. Which is almost never vim's fault. More likely an ssh connection was terminated or something along those lines.