VIM: RSync on save

大城市里の小女人 提交于 2019-12-03 03:43:50

You can hook into BufWritePost. I don't remember the exact syntax, but ":he BufWritePost" and ":he autocommand" should help you.

You setup a basic hook like this (pattern matches on a file, so you can do *.your_extension):

 :au BufWritePost * !rsync

Also, there should be some way to make it buffer-local.

If you want to keep the local copy, then the BufWritePost autocmd suggested by viraptor is the best bet, but assuming you have the netrw plugin installed (part of the default Vim runtime files), you can also start vim with:

vim rsync://myrsync_server.com/path

and just edit the file as if it's local: saves will update the remote server.

:help rsync

You can setup buffer local autocommands to do this (see :help autocmd-buflocal)

This sets vim to invoke rsync after each write to the buffer.

au BufWritePost,FileWritePost <buffer> silent !rsync -a /path/to/somedir/ user@host:/remote/dir/ --exclude=*.sw?

One caveat is that you must set one of these autocommands for each file you edit in a multi-file project. That gets cumbersome and it's easy to forget to set this up for new files/buffers - off course, you can setup a normal autocommand but then you have rsync firing off for writes to files not in the project.

With rsync-over-ssh, this works if the remotehost is accessible over a low-latency link - and perhaps not so well otherwise as each write blocks the buffer from futher updates until rsync has completed. To make the experience better, I recommend either

  • Reusing an SSH connection using connection-multiplexing over a persistent connection.
  • Sending rsync into the background by appending a & at the end of the command. YMMV.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!