Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore Vim Backups

Vim's file backup system just saved my proverbial @$$ but I have a question.

I have vim saving backups to ~/.vim/backups

To restore them I went to the directory and (sorted by date) copied the files I needed back to the necessary directories in my project folder. Easy enough, there were only 5 files. However, I'm surprised there's no obvious way to find which directory each file came from. I tried using vim -r path/to/file but that seems to use the swap and not the backup file. Since in my case vim didn't crash (I just mistakenly overwrote the files) there is no swap for these files.

So the main question is: What is the best way to restore vim backup files?

Side question: What happens in the .vim/backup/ directory when I have two same-name files from different paths (e.g. /path/one/file.html and /path/two/file.html)?

like image 697
Tron Avatar asked Jul 14 '11 18:07

Tron


People also ask

How do I recover a Vim file?

You can retrieve most of your changes to this file using the "recover" command of the editor. An easy way to do this is to give the command "vi -r myfile. txt". This method also works using "ex" and "edit".

Where are Vim backup files?

The backupdir is a comma-separated list of directories in which Vim creates backup files. For example, if you want backups to always be created in your system's temporary directory, set backupdir to "C:\TEMP" for Windows or "/tmp" for Unix and Linux.

How do I restore my backup files?

Right-click the Start button, then select Control Panel > System and Maintenance > Backup and Restore. Do one of the following: To restore your files, choose Restore my files. To restore the files of all users, choose Restore all users' files.


1 Answers

To answer the question about restoring, Vim doesn't know anything about the backups. It's backup system is very simple -- write a file. To restore, you have to do it manually.

However, you can add more info to your backups to make it easier to restore.

Ideally, the backupdir would honor trailing // to force it to expand the path of the file and replace / with %. e.g. /tmp/foo.rb would become the backup file `tmp%foo.rb'.

See :help undodir and :help directory for the undo and *.swp directory settings which do take //.

But we can fake this!

" Prevent backups from overwriting each other. The naming is weird,
" since I'm using the 'backupext' variable to append the path.
" So the file '/home/docwhat/.vimrc' becomes '.vimrc%home%docwhat~'
au BufWritePre * let &backupext ='@'.substitute(substitute(substitute(expand('%:p:h'), '/', '%', 'g'), '\', '%', 'g'),  ':', '', 'g').'~'

The backup files read filename-then-directory but it does work and should be sufficiently unique.

Example: .vimrc%home%docwhat~ is as backup of /home/docwhat/.vimrc

If you wanted minute by minute backups, so you loose less work, you can append a timestamp to the file name:

au BufWritePre * let &backupext = substitute(expand('%:p:h'), '/', '%', 'g') . '%' . strftime('%FT%T') .  '~'

But I'd recommend you setup a cron job to clean this directory regularly. If you edit as many files of me, this file would become huge quickly.

Alternatively, you could do something clever and change the backupdir before writing (with the BufWritePre hook), but that level of cleverness with vim is beyond me (at the moment).

Ciao!

like image 88
docwhat Avatar answered Oct 02 '22 22:10

docwhat