恢复 Vim 备份

发布于 2024-11-20 00:06:46 字数 468 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

比忠 2024-11-27 00:06:46

要回答有关恢复的问题,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 挂钩),但 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 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!

吲‖鸣 2024-11-27 00:06:46

据我所知,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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文