vim-vsnip自定义片段

插件vim-vsnip自带了相当数量的补全插件,但是一般都是通用的片段,对于编写自己常用的文件还需要自定义片段。本文记录自定义片段的具体过程。

安装插件

~/.config/nvim/lua/lazy-init.lua
1
2
3
4
5
6
7
require("lazy").setup({
...
'hrsh7th/cmp-vsnip' ,
'hrsh7th/vim-vsnip' ,
'rafamadriz/friendly-snippets' ,
...
})

编辑片段

这里可以使用命令VsnipOpen, 或者直接编辑~/.vsnip/markdown.json就可以直接添加markdown相应的片段。在编写过程中,为了适应Next博客主题输入数学公式的需要,需要输入$$, 但是这个片段是错误的,因为默认$是变量的符号,于是转义,片段的转义符号为\\, 于是得相应片段为:

~/.vsnip/markdown.json
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
{
"math-sup": {
"Vsnips.EnableAutoTrigger": true,
"prefix": ["^{"],
"body": [
"^{$1}<++>"
],
"description": "math formular"
},
"math-sub": {
"Vsnips.EnableAutoTrigger": true,
"prefix": ["_{"],
"body": [
"_{$1}<++>"
],
"description": "math formular"
},
"math-dkuohao": {
"Vsnips.EnableAutoTrigger": true,
"prefix": ["{"],
"body": [
"{$1}<++>"
],
"description": "math formular"
},
"math-fkuohao": {
"Vsnips.EnableAutoTrigger": true,
"prefix": ["["],
"body": [
"[$1]<++>"
],
"description": "math formular"
},
"math-ykuohao": {
"Vsnips.EnableAutoTrigger": true,
"prefix": ["("],
"body": [
"($1)<++>"
],
"description": "math formular"
},
"math-sqrt": {
"Vsnips.EnableAutoTrigger": true,
"prefix": ["sqrt"],
"body": [
"\\sqrt{$1}<++>"
],
"description": "math formular"
},
"math-frac": {
"Vsnips.EnableAutoTrigger": true,
"prefix": ["frac"],
"body": [
"\\frac{$1}{<++>}<++>"
],
"description": "math formular"
},
"math-tag": {
"prefix": ["tag"],
"body": [
"\\$\\tag{$1}\\label{tag<++>}\\$<++>"
],
"description": "math referance"
},
"math-eqref": {
"prefix": ["eqref"],
"body": [
"\\$\\eqref{eq:$1}\\$<++>"
],
"description": "math referance"
},
"math-label": {
"prefix": ["eqlabel"],
"body": [
"\\label{eq:$1}<++>"
],
"description": "math labels"
},
"math-multi2": {
"prefix": ["align"],
"body": [
"\\$\\$\\begin{align}",
"$1",
"\\end{align}\\$\\$<++>"
],
"description": "Multi-line Equations"
},
"math-multi1": {
"prefix": ["mequation"],
"body": [
"\\$\\$\\begin{equation}\\label{eq:$1}",
"\\begin{aligned}",
"<++>",
"\\end{aligned}",
"\\end{equation}\\$\\$<++>"
],
"description": "Multi-line Equations"
},
"math-simple": {
"prefix": ["equation"],
"body": [
"\\$\\$\\begin{equation}\\label{eq:$1}",
"<++>",
"\\end{equation}\\$\\$<++>"
],
"description": "Simple Equation"
},
"LinkGrid": {
"prefix": ["linkgrid"],
"body": [
"{% linkgrid %}",
" $1 | https://<+url+> | <+description+> | /Picture/<++>",
"{% endlinkgrid %}"
],
"description": "For diary.sh write"
},
"button": {
"prefix": ["button"],
"body": [
"{% button https://<+url+>, $1 %}"
],
"description": "For diary.sh write"
},
"fold": {
"prefix": ["fold"],
"body": [
"<details><summary>$1</summary>",
"<+contents+>",
"</details>"
],
"description": "For diary.sh write"
},
"more": {
"prefix": ["more"],
"body": [
"<!-- more -->"
],
"description": "For diary.sh write"
},
"arrow": {
"prefix": ["arrow"],
"body": [
"${1|&rarr;,&uarr;,&darr;,&larr;,&harr;|}"
]
},
"picture": {
"prefix": ["picture"],
"body": [
"![$1](/Picture/<+pic+> \"<+title+>\")<++>"
]
},
"centertext": {
"prefix": ["center"],
"body": [
"{% cq %}",
"$1",
"{% endcq %}",
"<++>"
]
},
"textcolor": {
"prefix": ["color"],
"body": [
"<label style=\"color:${1|red,yellow,blue,cyan,gold,green,gray,orange|}\"><++></label><++>"
]
},
"underline": {
"prefix": ["underline"],
"body": [
"<u>$1</u><++>"
]
},
"sub": {
"prefix": ["__"],
"body": [
"<sub>$1</sub><++>"
]
},
"sup": {
"prefix": ["^^"],
"body": [
"<sup>$1</sup><++>"
]
},
"href": {
"prefix": ["ref"],
"body": [
"[$1](/<+year+>/<+month+>/<+day+>/<+article+>)"
],
"description": "Ref the article of my blog"
},
"hurl": {
"prefix": ["url"],
"body": [
"[$1](<+url+>)<++>"
]
},
"delete": {
"prefix": ["delete"],
"body": [
"~~$1~~"
]
},
"code": {
"prefix": ["code"],
"body": [
"{% code <+title+> lang:${1|sh,python,lua,perl,yaml,ruby|} %}",
"<+code+>",
"{% endcode %}",
"<++>"
],
"description": "Next theme environment for Hexo"
},
"codeblock": {
"prefix": ["codeblock"],
"body": [
"{% codeblock <+title+> lang:${1|sh,python,lua,perl,yaml,ruby|} %}",
"<+code+>",
"{% endcodeblock %}",
"<++>"
],
"description": "Next theme environment for Hexo"
},
"```": {
"prefix": ["```"],
"body": [
"```${1|sh,python,lua,perl,yaml,ruby|} <+title+> ",
"<+code+>",
"```",
"<++>"
],
"description": "Next theme environment for Hexo"
},
"note": {
"prefix": ["note"],
"body": [
"{% note ${1|default,primary,success,info,warning,danger|} %}",
"<++>",
"{% endnote %}",
"<++>"
],
"description": "Next theme environment for Hexo"
},
"tabs": {
"prefix": ["tabs"],
"body": [
"{% tabs ${1:<+UniqueName+>} %}",
"<!-- tab ${2:<+tabcaption+>} -->",
"<++>",
"<!-- endtab -->",
"<++>",
"{% endtabs %}",
"<++>"
],
"description": "Next theme environment for Hexo"
},
"tab": {
"prefix": ["tab"],
"body": [
"<!-- tab ${2:<+tabcaption+>} -->",
"<++>",
"<!-- endtab -->",
"<++>"
],
"description": "Next theme environment for Hexo"
},
"label": {
"prefix": ["label"],
"body": [
"{% label ${1|default,primary,success,info,warning,danger|}@${2:<+content+>} %}<++>"
],
"description": "Next theme environment for Hexo"
},
"centerquote": {
"prefix": ["centerquote"],
"body": [
"{% centerquote %}",
"${1:<+content+>}",
"{% endcenterquote %}",
"<++>"
],
"description": "Next theme environment for Hexo"
},
"video": {
"prefix": ["video"],
"body": [
"{% video ${1|/Video/<++>,https://<+website+>|}%}<++>"
],
"description": "Next theme environment for Hexo"
},

"pdf": {
"prefix": ["pdf"],
"body": [
"{% pdf ${1|/PDF/<++>.pdf,https://<+website+>|}%}<++>"
],
"description": "Next theme environment for Hexo"
},
"grouppicture": {
"prefix": ["grouppicture"],
"body": [
"{% grouppicture ${1:<+number+>}-${2:<+layout+>} }%}",
"![${3:<+picturetitle+>}](${4:<+picture+>})",
"{% endgroudpicture %}",
"<++>"
],
"description": "Next theme environment for Hexo"
}
}

参考资料