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!
Footnotes:
1. Which is almost never vim's fault. More likely an ssh connection was terminated or something along those lines.
Labels: vim
3 Comments:
Stuff that is useful to perl people is also good.
My usual solution is to have a per-project dtach session on the dev server though - that way my vi session pretty much never die.
-- mst
If you have get these due to ssh connection drops, take some time to learn how to use GNU Screen. It is great for exactly this situation. If your connection drops, it keeps the application running and you can reattach to your session once you have reconnected.
Reenen, I have used screen for years and love it. Bash, screen and vim make up my ideal development environment. Unfortunately, screen has a few glitches under cygwin, and that is where this script had been most useful. Now that I am 100% linux and os/x, I haven't used it in quite a while.
Good suggestion though.
Post a Comment
Subscribe to Post Comments [Atom]
<< Home