RSync on save in VIM

前端 未结 3 1301
猫巷女王i
猫巷女王i 2021-02-06 11:00

I\'m looking for a way to map the :w command so that when I use it, it will rsync the current directory and save the file in question. Ideally, the response generated by the rsy

3条回答
  •  醉话见心
    2021-02-06 11:28

    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  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.

提交回复
热议问题