Wikia

Vim Tips Wiki

Write multiple copies of the currently edited file

Talk0
1,613pages on
this wiki
Revision as of 10:41, January 31, 2014 by JohnBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created November 21, 2013 · complexity basic · author Lonetwin · version 7.0

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

CommentsEdit

Around Wikia's network

Random Wiki