title | author | ms.author | ms.date | ms.topic | ms.assetid |
---|---|---|---|---|---|
Code Snippets |
asb3993 |
amburns |
04/14/2017 |
article |
0FE27C0C-A861-4133-A74E-8D0505CF5342 |
Code snippets, often referred to as code templates, are useful for efficient programming as they allow the insertion and editing of pre-written blocks of code. Using code snippets can be particularly convenient for quickly adding common patterns, or even for learning new patterns when as the developer you are unsure of syntax. There are templates provided for C#, F#, HTML, XML, Python, and Razor.
This section will explain how to create, insert, and use snippets in code.
There are some different ways to add code snippets, some of which are described below:
-
Tab Expansion - Start typing the template name, select it from the list and press TAB TAB to add it:
-
Toolbox - Use the toolbox pad to display a list of all code snippets. Drag any template from the toolbox into the correct position in the source code:
- Insert Templates command - There is currently no default key binding set for inserting a template. To create one, browse to Visual Studio > Preferences... > Key Bindings and search for
template
. This allows adding the desired key binding into the Edit Binding field, then click Apply:
While there are many existing templates in a variety of languages that you can use and edit, new templates can also be added by navigating to Visual Studio > Preferences > Text Editor > Code Snippets: