Vim Tips Wiki

Write multiple copies of the currently edited file

Revision as of 19:40, November 21, 2013 by (Talk)

1,619pages on
this wiki

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


and you need to sync the changes in this file to


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

  /mnt/sshfs/host0/ and

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.

Around Wikia's network

Random Wiki