Linux – Auto Recovery and Deletion of VIM .swp Files

linuxvim

So, if I have a screen session with maybe 15 files open, in about 10 instances of VIM, and my landlord yanks my power …

Can I load everything up and recover the swp files, and delete them after in an automated way. More importantly, is this not a smart thing to do for some reason?

Best Answer

You can easily open them all up by doing something like

for swpfile in .*.swp; do
    swpfile="${swpfile#.}"    # strip the initial "."
    vim "${swpfile%.swp}"     # strip the extension
done

and doing whatever you like in Vim (probably a lot of (r)ecover enter enter :wq, I assume).

You can also do this from within Vim using the :recover command - write a quick script to list the files and call recover for each (you may even be able to silence the prompt) then save. I don't remember well enough to do it this way off the top of my head, though.