为了回答关于恢复的问题,Vim 对备份一无所知。它的备份系统非常简单——写入一个文件。要恢复,你必须手动操作。
但是,你可以添加更多信息到你的备份中,以便更容易地进行恢复。
理想情况下,`backupdir` 应该支持末尾的 `//` 以强制扩展文件路径并将 `/` 替换为 `%`。例如,`/tmp/foo.rb` 将变成备份文件 `tmp%foo.rb`。
请参见 `:help undodir` 和 `:help directory`,了解 `undo` 和 `*.swp` 目录设置的详细信息,其中确实包括了 `//`。
但是我们可以伪造出这个功能!
" 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').'~'
备份文件读取的是文件名-然后目录,但它可以工作并且应该足够唯一。
例如:.vimrc%home%docwhat〜
是 /home/docwhat/.vimrc
的备份。
如果您想要每分钟备份,以便您减少工作量,则可以在文件名后附加时间戳:
au BufWritePre * let &backupext = substitute(expand('%:p:h'), '/', '%', 'g') . '%' . strftime('%FT%T') . '~'
但我建议您设置一个定期清理此目录的cron作业。如果您编辑了我的许多文件,这个文件会很快变得非常大。
或者,您可以聪明地在写入之前更改backupdir
(使用BufWritePre
钩子),但是那种水平的聪明才智对我来说还有待提高(目前)。
Ciao!