-
Notifications
You must be signed in to change notification settings - Fork 22
/
02_using_templates.html
104 lines (82 loc) · 4.22 KB
/
02_using_templates.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<h3 id="using-templates">Using Templates</h3>
<h4>Problem</h4>
<p>You create documents containing boilerplate text and would like to avoid
typing it.</p>
<p>For instance, you write HTML documents and don't want to type the standard
preamble every time.</p>
<h4>Solution</h4>
<p>Use templates and associate them with a file extension. When a new file is
created with an extension for which a template is available, the template
contents will be inserted.</p>
<p>You must first create a skeleton document, e.g.:</p>
<pre><code><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd"
xml:lang="en">
<head>
<title>Document Title</title>
</head>
<body></body>
</html></code></pre>
<p>Save this document in your Vim directory with the corresponding file
extension, e.g. <tt>~/.vim/skel/tmpl.html</tt>. (You may also want to make
this file read only so you don't modify it by mistake).</p>
<p>Add the following to your <tt>.vimrc</tt>:</p>
<pre><code>autocmd! BufNewFile * silent! 0r ~/.vim/skel/tmpl.%:e</code></pre>
<p>Now, every time you open a new file Vim checks <tt>~/.vim/skel/</tt> for a
file whose name starts with <tt>tmpl.</tt> and ends with the extension of the
file you're creating. It then reads the template file into your new file's
buffer.</p>
<p>For example, if you added the template above then invoked Vim with <tt>vim
page.html</tt>, the XHTML above would be inserted into the
<tt>page.html</tt> file automatically.</p>
<h4>Discussion</h4>
<p>Once you have created a document from a template you have to insert text at
various predefined positions. For instance, for the XHTML template, you have
to change the contents of <code><title>…</title></code>,
then start typing between the <code><body>…</body></code>
tags. Navigating between these points in the document, which are the same
every time you use the template, is cumbersome. </p>
<p>The traditional solution is to use placeholders. This involves including some
notation in the template file that indicates where your input is required,
then providing a method to jump between them. </p>
<p>Here's the previous template with placeholders added:</p>
<pre><code><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd"
xml:lang="en">
<head>
<title>%TITLE%</title>
</head>
<body>%BODY%</body>
</html></code></pre>
<p>The placeholder notation is arbitrary, but let's stick with
<tt>%VARIABLE%</tt> for the sake of example. Now we need a way to jump between
them, and <kbd><kbd><Ctrl></kbd>-<kbd>p</kbd></kbd> (menmonic:
<i>p</i>laceholder) seems reasonable. While we're at it, we'll define a
function (<tt>LoadTemplate()</tt>) to replace the <tt>autocmd</tt> line we
added earlier. Add the following to your <tt>vimrc</tt>:</p>
<pre><code>function! LoadTemplate()
silent! 0r ~/.vim/skel/tmpl.%:e
" Highlight %VAR% placeholders with the Todo colour group
syn match Todo "%\u\+%" containedIn=ALL
endfunction
autocmd! BufNewFile * call LoadTemplate()
"Jump between %VAR% placeholders in Normal mode with
" <Ctrl-p>
nnoremap <c-p> /%\u.\{-1,}%<cr>c/%/e<cr>
"Jump between %VAR% placeholders in Insert mode with
" <Ctrl-p>
inoremap <c-p> <ESC>/%\u.\{-1,}%<cr>c/%/e<cr></code></pre>
<p>If we create a new HTML file now this is what we see:</p>
<img src="vim-html-template.png" width="580" height="202"
alt="Screenshot of GVim displaying a template for a HTML document.">
<p>See <a href="#inserting-snippets">below</a> for ways to automatically jump
to the placeholders and for inserting smaller code fragments by using
snippets.</p>