Wikia

Vim Tips Wiki

Changes: Write multiple copies of the currently edited file

Edit

Back to page

(Insert TipProposed template + manual clean)
 
Line 1: Line 1:
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
+
{{TipProposed
src/mysite/index.html
+
|id=0
and you need to sync the changes in this file to
+
|previous=0
/var/www/mysite/index.html
+
|next=0
after each edit
+
|created=November 21, 2013
OR
+
|complexity=basic
You have mounted two hosts using sshfs as:
+
|author=Lonetwin
/mnt/sshfs/host0/ and
+
|version=7.0
/mnt/sshfs/host1/
+
|subpage=/2013
and you'd like to simultaneously edit a file <code>path/to/file.txt</code> at the same location under the two directories, you can open the file <code>src/mysite/index.html</code> or <code>path/to/file.txt</code> using vim and then before any edits, run the command:
+
|category1=File Handling
  +
|category2=
  +
}}
  +
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
  +
<pre>
  +
src/mysite/index.html
  +
</pre>
  +
  +
And you need to sync the changes in this file to
  +
<pre>
  +
/var/www/mysite/index.html
  +
</pre>
  +
  +
after each edit, or you have mounted two hosts using sshfs as:
  +
<pre>
  +
/mnt/sshfs/host0/ and
  +
/mnt/sshfs/host1/
  +
</pre>
  +
  +
and you'd like to simultaneously edit a file <code>path/to/file.txt</code> at the same location under the two directories, you can open the file <code>src/mysite/index.html</code> or <code>path/to/file.txt</code> using Vim and then before any edits, run the command:
 
<source lang='vim'>
 
<source lang='vim'>
:autocmd BufWritePost <buffer> w! <path where the duplicate would be written>/%
+
:autocmd BufWritePost <buffer> w! <path where the duplicate would be written>/%
 
</source>
 
</source>
  +
 
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.
 
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:
 
If you happen to need this functionality often, you can add a custom command to your vimrc, like so:
 
<source lang='vim'>
 
<source lang='vim'>
command -nargs=1 -complete=dir DuplicateAt autocmd BufWritePost <buffer> w! <args>/%
+
command -nargs=1 -complete=dir DuplicateAt autocmd BufWritePost <buffer> w! <args>/%
 
</source>
 
</source>
   
 
This will define a new command <code>DuplicateAt</code> which can be invoked as:
 
This will define a new command <code>DuplicateAt</code> which can be invoked as:
 
<source lang='vim'>
 
<source lang='vim'>
:DuplicateAt /path/to/duplicate/at/
+
:DuplicateAt /path/to/duplicate/at/
 
</source>
 
</source>
after which any writes to the current buffer will also be written to the file path of the current buffer under the directory <code>/path/to/duplicate/at/</code>.
+
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.
+
After which any writes to the current buffer will also be written to the file path of the current buffer under the directory <code>/path/to/duplicate/at/</code>.
[[Category:File Handling]]
+
  +
==Comments==

Latest revision as of 10:41, January 31, 2014

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