Vim Tips Wiki
Advertisement

Sometimes you may need to keep multiple copies of the same file at different locations in sync. For instance, if you are editing the file

 src/mysite/index.html

and you need to sync the changes in this file to

 /var/www/mysite/index.html 

after each edit OR You have mounted two hosts using sshfs as:

  /mnt/sshfs/host0/ and
  /mnt/sshfs/host1/

and you'd like to simultaneously edit a file path/to/file.txt at the same location under the two directories, you can open the file src/mysite/index.html or path/to/file.txt using vim and then before any edits, run the command:

  :autocmd BufWritePost <buffer> w! <path where the duplicate would be written>/%

This will ensure that everytime you execute write the changes to the current buffer, the same changes will also be written to the path for the duplicate of the file.

If you happen to need this functionality often, you can add a custom command to your vimrc, like so:

  command -nargs=1 -complete=dir DuplicateAt autocmd BufWritePost <buffer> w! <args>/%

This will define a new command DuplicateAt which can be invoked as:

 :DuplicateAt /path/to/duplicate/at/

after which any writes to the current buffer will also be written to the file path of the current buffer under the directory /path/to/duplicate/at/. Hope this tip is useful to some. I use it all the time when coding on distributed apps where I mount the sources for the application directory over sshfs.

Advertisement