说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家!
当我们在Visual Studio中创建.c文件或者.cpp文件时,每次都要去重复手写一些常见的头文件和main函数,非常麻烦,为了提高我们写代码的效率,我们可以将这些代码设置为代码块,然后直接使用快捷方式就可以生成你定义的这段代码,如下博主在.c文件中输入#1后按下Tab键,就快速生成了自定义的一些头文件和main函数了,效果如下:

第一步: 你需要新建两个后缀名为.snippet的文件,名字随便起,比如博主这里是c.snippet 和 cpp.snippet,顾名思义就是.c文件和.cpp文件使用的代码片段,内容如下:
c.snippet文件
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2019/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>c</Title>
<Shortcut>#1</Shortcut>
<Description>c 初始化</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Code Language="cpp"><![CDATA[#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
int main()
{
$selected$ $end$
system("pause");
return EXIT_SUCCESS;
}]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
cpp.snippet文件
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2019/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>cpp</Title>
<Shortcut>#2</Shortcut>
<Description>c++ 初始化</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
</Declarations>
<Code Language="cpp"><![CDATA[#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
$selected$ $end$
system("pause");
return EXIT_SUCCESS;
}]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
关于.snippet文件说明:
:文件的描述信息,在VS端显示。
:自动补全提示符(此处即为输入”#1"或“#2”,按下Tab键自动补全)
:补全的代码
第二步: 打开/回到Visual Studio编译器界面,点击>>工具>>代码段管理器,语言记得选择Visual C++,然后选中My Code Snippets,点击下方的导入,添加你创建的两个.snippet文件,最后点击完成+确定即可。


完成以上两步骤后,你就可以在.c或.cpp文件中,输入#1或#2,再按下Tab键,就可以自动生成对应的代码块了!
