13

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

Tron
  • 693
  • 1
  • 6
  • 17
  • Actually, it looks like the side question has come back to bite me. One of the backups did overwrite the other and I don't have any record of /path/two/file.html ... sigh. – Tron Jul 14 '11 at 19:25
  • you can find something by reading file ~/.viminfo to see some editing history. – taro Jul 15 '11 at 09:24

2 Answers2

11

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!

dougd_in_nc
  • 371
  • 5
  • 20
docwhat
  • 11,435
  • 6
  • 55
  • 54
4

From what I can tell, Vim doesn't save any information at all regarding the file's original location.

Saving a backup of two same-name files could either overwrite the existing backup or use a different name, depending on what's set in your .vimrc. The option 'backup' will overwrite an existing backup file, but 'writebackup' will re-name a new backup in order to avoid an overwrite.

Check out Vim's documentation for more info - :help backup

damien
  • 902
  • 1
  • 7
  • 12