要回答有关恢复的问题,Vim 对备份一无所知。它的备份系统非常简单——写一个文件。要恢复,您必须手动执行。
但是,您可以向备份添加更多信息,以便更轻松地恢复。
理想情况下,backupdir
将尊重尾随//
强制它扩展文件的路径并替换/
with %
. e.g. /tmp/foo.rb
将成为备份文件“tmp%foo.rb”。
See :help undodir
and :help directory
为了undo
and *.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
hook),但 vim 的聪明程度超出了我的范围(目前)。
Ciao!