恢复 Vim 备份
Vim 的文件备份系统刚刚保存了我众所周知的@$$,但我有一个问题。
我有 vim 将备份保存到 ~/.vim/backups
为了恢复它们,我转到该目录并(按日期排序)将我需要的文件复制回项目文件夹中的必要目录。很简单,只有 5 个文件。然而,令我惊讶的是没有明显的方法来查找每个文件来自哪个目录。我尝试使用 vim -r path/to/file ,但这似乎使用交换而不是备份文件。因为在我的例子中,vim 没有崩溃(我只是错误地覆盖了文件),所以这些文件没有交换。
所以主要问题是: 恢复 vim 备份 文件的最佳方法是什么?
附带问题:当我有两个相同的文件时,.vim/backup/ 目录中会发生什么?命名来自不同路径的文件(例如 /path/one/file.html
和 /path/two/file.html
)?
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
)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要回答有关恢复的问题,Vim 对备份一无所知。它的备份系统非常简单——写一个文件。要恢复,您必须手动执行。
但是,您可以向备份添加更多信息,以便更轻松地恢复。
理想情况下,
backupdir
将遵循尾随//
以强制其扩展文件路径并将/
替换为%.例如
/tmp/foo.rb
将成为备份文件“tmp%foo.rb”。请参阅
:help undodir
和:help directory
了解undo
和*.swp
目录设置,这些设置需要>//。
但我们可以伪造这个!
备份文件先读取文件名,然后读取目录,但它确实有效并且应该足够唯一。
示例:
.vimrc%home%docwhat~
是/home/docwhat/.vimrc
的备份如果您想要每分钟备份,这样可以减少工作量,您可以在文件名中附加时间戳:
但我建议您设置一个 cron 作业来定期清理此目录。如果你编辑我的许多文件,这个文件很快就会变得很大。
或者,您可以做一些聪明的事情,并在写入之前更改
backupdir
(使用BufWritePre
挂钩),但 vim 的聪明程度超出了我的范围(目前)。再见!
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 theundo
and*.swp
directory settings which do take//
.But we can fake this!
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:
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 theBufWritePre
hook), but that level of cleverness with vim is beyond me (at the moment).Ciao!
据我所知,Vim 根本不保存有关文件原始位置的任何信息。
保存两个同名文件的备份可能会覆盖现有备份或使用不同的名称,具体取决于 .vimrc 中的设置。选项“backup”将覆盖现有备份文件,但“writebackup”将重新命名新备份以避免覆盖。
查看 Vim 的文档以获取更多信息 - :help backup
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