我使用 vim 和 netrw 插件远程处理许多文件。 我还通过更改 &backupext
(在 文档)。
在处理远程文件 (scp://server//folder/file.txt) 时,我注意到当我保存它时,甚至没有进行备份。
有没有一种方法可以让我每次保存远程文件时自动在本地进行备份(根据 vim 备份设置)?
I work with many files remotely using vim and the netrw plugin. I also keep timestamped backups by changing the &backupext
(found in the documentation).
While working in a remote file (scp://server//folder/file.txt), I noticed that when I save it, a backup isn't even being made.
Is there a way I can have a backup automatically made locally (according to vim backup settings) every time I save a remote file?
发布评论
评论(5)
我认为没有一个简单的设置可以切换。 您可以尝试编写一些脚本 - 添加您自己的
BufWriteCmd
自动命令来写入备份。 尝试使用set verbose=9
进行调试(并查看 netrw 是如何做到的)。I don't think there's a simple setting to toggle. You could try to script something though - add your own
BufWriteCmd
autocommand to write a backup. Try usingset verbose=9
for debugging (and to see how netrw does it).我曾经遇到过这个问题的一个变体:在单个远程计算机上保存我对单个源代码树所做的更改的本地记录。
我维护了整个源代码树的本地副本和本地 svn 存储库来对文件进行版本控制。 我没有使用 netrw 并寻找在本地保存备份的方法,而是在本地编辑了所有内容,并且需要一种方法将我的更改自动传播到远程计算机。
解决方案 1:使用 BufWritePost 自动命令调用 scp 在写入文件后复制文件。 如果系统允许进程共享文件描述符,那么这可以足够有效地工作,因为可以运行到远程计算机的主 ssh 会话并共享后续会话的连接。 如果您不太幸运(当时我在 Windows 计算机上工作),则为每个文件协商新连接所需的时间可能会很痛苦
解决方案 2:对于 Windows,请使用 WinSCP,它具有“保持远程计算机更新”模式,在该模式下,它监视目录及其所有子目录的更改,然后在给定一组的情况下自动传播更改规则(要忽略的模式、不同文件类型的传输模式等)。
I once struggled with a variant of this problem: keeping local records of changes I made to a single source tree on a single remote machine.
I maintained a local copy of the entire source tree and a local svn repository to version the files. Instead of using netrw and looking for a way to save backups locally, I edited everything locally and needed a way to automatically propagate my changes to the remote machine.
solution 1: use a BufWritePost Autocommand to invoke scp to copy the file after it's been written. This can work efficiently enough if the system allows processes to share file descriptors, since it's possible to run a master ssh session to the remote machine and share the connection for subsequent sessions. If you are less fortunate (I was working on a windows machine at the time), the time it takes to negotiate a new connection for each file can be painful
solution 2: for windows, use WinSCP, which has a "keep remote machine updated" mode, in which it monitors a directory and all its subdirectories for changes and then automatically propagates the changes, given a set of rules (patterns to ignore, transfer mode for different filetypes, etc).
从您的 scp 协议使用情况来看,使用 fuse 似乎会更容易sshfs,这将使您能够拥有支持您的正常文件系统行为,而不仅仅是推/拉类型的 CRUD 接口。
Well judging from your scp protocol use, it seems that it would be a lot easier for you to be using the fuse sshfs, which would enable you to have normal filesystem behaviour backing you instead of just a push/pull type CRUD interface.
netrw 插件完全绕过
:write
命令,恢复“备份”功能的唯一方法是使用自动命令自行完成。The netrw plugin completely bypasses the
:write
command, the only way to get the 'backup' functionality back is to do it yourself using autocommands.我意识到这不是对原始问题的直接答案 - 但是,您所做的事情看起来很像一个简单的变相 VCS - 那么您是否有理由不将远程文件(例如 git 存储库)制作出来,然后在本地克隆该存储库并在编辑后推送更改?
I realise it's not a direct answer to the original question - however, what you are doing seems a lot like a simple-VCS-in-disguise - so is there a reason why you would not make the remote files e.g. a git repository, then clone that repository locally and push the changes after the editing ?