Visual Studio 中自定义代码片段(Snippet)的核心语法规则和常用的自定义代码片段+如何导入自定义代码片段

如果嫌打cout和打主函数太麻烦又不想装插件的情况下,那么可以自定义代码片段提高开发效率,下面就就简单介绍一下自定义代码片段的语法规则,然后列举一些常用的自定义代码片段。

一、代码片段文件基础结构 



   
    
代码片段标题 触发词 功能描述
变量名 默认值

二、关键元素讲解  

1.Header元素

必需字段:

  • :代码片段标题。如Class Template</li> <li><Shortcut>:触发词。如 for 触发</li> </ul> <p>可选字段:</p> <ul> <li><Description>:功能描述</li> <li><Author>:作者</li> </ul> <p><strong>2.Snippet元素 </strong></p> <ul> <li> <Declarations>:定义可编辑的占位符</li> </ul> <pre><code class="language-XML"><Declarations> <Literal Editable="true"> //是否允许修改 <ID>type</ID> //变量名字 <Default>int</Default> //默认值 </Literal> </Declarations></code></pre> <ul> <li><code >元素</li> </ul> <p>        必须用<code><![CDATA[ ]]></code>包裹代码,避免 XML 转义问题</p> <p>        支持多行代码,每行需独立定义</p> <p><strong>3.特殊语法标记 </strong></p> <table align="left" border="1" style="width:500px"> <thead> <tr> <th><strong>标记</strong></th> <th><strong>功能</strong></th> </tr> </thead> <tbody> <tr> <td>$变量名$</td> <td>引用<code>Declarations</code>定义的占位符(如<code>$className$</code>)</td> </tr> <tr> <td>$end$</td> <td>光标最终停留位置</td> </tr> </tbody> </table> <h3>三、具体实例(常用代码片段)</h3> <h4> cpp_main.snippet</h4> <pre><code class="language-XML"><?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title>cpp_main_clean main 纯净版C++主函数模板 using namespace std; int main() { $end$ return 0; }]]>

    cpp_cout_endl.snippet 

    
    
      
        
    cpp_cout_endl coutl 带换行的标准输出语句
    caption 输入要显示的内容 output

    getset.snippet 

    
    
      
        
    getset getset Generate getter and setter for C++
    type int Name Name name VariableName $name$ = $name$; } ]]>

    四、导入到自定义代码片段

    如下图所示,或者打开上面位置的目录,直接将.snippet文件拖入即可。 

    Visual Studio 中自定义代码片段(Snippet)的核心语法规则和常用的自定义代码片段+如何导入自定义代码片段_第1张图片

你可能感兴趣的:(C/C++,一些小技巧,visual,studio)