Tip #624: Insert template files into buffer ( HTML editing for example)
tip karma |
Rating 7/6, Viewed by 1640
|
Read and edit this tip on the
Vim tip wiki.
The wiki may have a more recent version of this tip.
created: |
|
December 18, 2003 4:35 |
|
complexity: |
|
intermediate |
author: |
|
A.L Marin |
|
as of Vim: |
|
5.7 |
While editing HTML I want to use template files to be expanded on my html pages. Say for example I have something like this on my html file:
<!--"header.html"-->
<p>html code here</p>
<p>more html code here...</p>
<!--"footer.tml"-->
I want the files "header.html" and "footer.html" to be inserted on my page, you can do this with the following global command:
:g%<!--"\w\+.\w\+"--\>%exe ":r " . matchstr(getline("."), "\\w\\+\\.\\w\\+")
<< External Paste Buffer |
Typing print statements faster and more ergonomically (esp in C++) >>
Additional Notes
User-command-REVIM,
December 20, 2003 14:30
|
Above does not work if full path of template must be specified (because only
word characters get matched -- excludes slashes, spaces, and semicolons).
Probably not a good idea to embed files in comments because they could be
easily overlooked later. Strive for generality in usage.
Suggest using
#revim=path/file
construct at the _beginning_ of a line
where revim stands for "read vim" (also close to "revise").
The suitable global command would be,
:g/^#revim=/exe ":r " . strpart(getline("."), 7) | normal kdd
Even more useful, would be to establish a user command Revim...
command! -range=% Revim <line1>,<line2>g/^#revim=/exe ":r " . strpart(getline("."), 7) | normal kdd
" 'Read_vim' / Revise by reading in file templates.
" Get the string part after the = sign, on line(s) which
" *begin* with #revim= , and use that to read in said file
" -------------------- (kdd deletes the # line; put comments in templates).
Usage example:
#revim=c:/templates/header-html.txt
Executing user command Revim replaces the # line by reading in said file.
|
Anonymous,
December 20, 2003 17:14
|
Great ideas!
I changed the name of the user command to Refile -- to remind
me to rename the file after the alterations, and to keep the
reference file with the #refile=path/file
lines intact...
refile > read ref(erence) file(s)
This would require changing that magic number from 7 to 8,
because "refile" has one more letter than "revim" ...
|
|