首页 > 解决方案 > 自定义 Vim 写入功能以制作备份副本

问题描述

我想在 Vim 中创建一个 write 函数,它执行以下操作:

这样我可以查看较旧的更改,如果文件被意外删除,我将始终拥有最新的备份。有谁知道如何做到这一点或向我推荐一些资源,以便我自己弄清楚。谢谢

标签: vimbackup

解决方案


I would highly recommend some form of version control, like git or mercurial. This will give you the file history that you want (bonus: branching) and will also provide easy way to clone and share history between different repositories.

Git

You can use a plugin like vim-futitive to handle staging, commits, and many other git functions. Vimcasts provides some a nice screencast tutorial for fugitive:

Persistent Undo

To go along with version control, Vim also provides persistent undo where it save's Vim's undo history to a file. See :h persistent-undo.

Simply set 'undofile' in your vimrc:

set undofile

May want to set 'undodir' to a different location, e.g. set undodir=~/.local/vim/undo. Note: 'undodir' must exist.

It should be noted persistent undo is not version control and should not be treated as such. For example undo history can easily be messed up by editing a file in a different editor.

Plugins like Gundo and undotree may help in navigating deep in the past or complicated undo histories.


推荐阅读