自定义代码片段
新建一个脚本,命名为,xxx.snippet
,本例为#1.snippet
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>#1</Title>
<Shortcut>#1</Shortcut>
<Description>c语言main函数</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>expression</ID>
<ToolTip>要计算的表达式</ToolTip>
<Default>true</Default>
</Literal>
</Declarations>
<Code Language="cpp"><![CDATA[#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>
int main(void)
{
$selected$$end$
system("pause");
return EXIT_SUCCESS;
}
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
仔细看一下这份XML其实可以发现关键的地方就三处.
-
上面的
Shortcut
标签定义了缩写字符串。 -
中间
Declaration
标签定义了所需要的变量。 -
<Code>xxx</Code>
标签定义了输出的格式。
打开代码片段管理器
选择语言,并导入脚本
选择存储位置,完成配置
测试使用
输入#1
按TAB
或ENTER
,自动导入模板中的代码如下