Walkthrough: Create a code snippet in Visual Studio
Applies to: Visual Studio Visual Studio for Mac Visual Studio Code
You can create a code snippet with only a few steps. All you need to do is create an XML file, fill in the appropriate elements, and add your code to it. You can optionally make use of replacement parameters and project references. Then, you can import the snippet to your Visual Studio installation by using the Import button in the Code Snippets Manager, which is available from the Tools menu.
The following XML is the basic snippet template. We'll walk you through creating a code snippet in Visual Studio by using this snippet template and then making modifications to it.
<?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title></Title> </Header> <Snippet> <Code Language=""> <![CDATA]> </Code> </Snippet> </CodeSnippet> </CodeSnippets>
Create a code snippet
Start Visual Studio. Then, in the Get started section of the Start window, select Continue without code.
From the menu bar, select File > New > File (or, enter the keyboard shortcut Ctrl+N) to open the New File dialog. Then, select XML File.
Add the template code shown in the Snippet template section of this article.
In the code, fill in the title of the snippet in the Title element. Use the title Square Root.
Fill in the language of the snippet in the Language attribute of the Code element. For C#, use CSharp, for Visual Basic, use VB, and for C++, use CPP.
To see all the available language values, browse the Code element attributes section on the Code snippets schema reference page.
Add the snippet code in the CDATA section inside the Code element.
<Code Language="CSharp"> <![CDATA[double root = Math.Sqrt(16);]]> </Code>
Or for Visual Basic:
<Code Language="VB"> <![CDATA[Dim root = Math.Sqrt(16)]]> </Code>
You can't specify how lines of code in the CDATA section of a code snippet should be indented or formatted. Upon insertion, the language service formats the inserted code automatically.
Save the snippet as SquareRoot.snippet (you can save it anywhere).
Import a code snippet
You can import a snippet to your Visual Studio installation by using the Code Snippets Manager. Open it by selecting Tools > Code Snippets Manager.
Select the Import button.
Go to the location where you saved the code snippet in the previous procedure, select it, and select Open.
The Import Code Snippet dialog opens, asking you to choose where to add the snippet from the choices in the right pane. One of the choices should be My Code Snippets. Select it, select Finish, and then select OK.
The snippet is copied to one of the following locations, depending on the code language and the version of Visual Studio that you're using:
%USERPROFILE%\Documents\Visual Studio 2019\Code Snippets\Visual C#\My Code Snippets %USERPROFILE%\Documents\Visual Studio 2019\Code Snippets\Visual Basic\My Code Snippets
%USERPROFILE%\Documents\Visual Studio 2022\Code Snippets\Visual C#\My Code Snippets %USERPROFILE%\Documents\Visual Studio 2022\Code Snippets\Visual Basic\My Code Snippets
Test your snippet by opening a C# or Visual Basic project. With a code file open in the editor, choose Snippets > Insert Snippet from the right-click menu, then My Code Snippets. You should see a snippet named Square Root. Double-click it.
The snippet code is inserted in the code file.
Description and shortcut fields
Description fields provide more information about your code snippet when viewed in the Code Snippets Manager. The shortcut is a tag that users can type in order to insert your snippet. Edit the snippet you've added by opening the file %USERPROFILE%\Documents\Visual Studio 2019\Code Snippets\[Visual C# or Visual Basic]\My Code Snippet\SquareRoot.snippet or %USERPROFILE%\Documents\Visual Studio 2022\Code Snippets\[Visual C# or Visual Basic]\My Code Snippet\SquareRoot.snippet.
Because you're editing the file in the directory where Visual Studio placed it, you don't need to reimport it to Visual Studio.
Add Author and Description elements to the Header element, and then fill them in.
Confirm that the Header element looks similar to the following example:
<Header> <Title>Square Root</Title> <Author>Myself</Author> <Description>Calculates the square root of 16.</Description> </Header>
From the Tools menu, open the Code Snippets Manager, and then select your code snippet. In the right pane, notice that the Description and Author fields are now populated.
To add a shortcut, add a Shortcut element within the Header element:
<Header> <Title>Square Root</Title> <Author>Myself</Author> <Description>Calculates the square root of 16.</Description> <Shortcut>sqrt</Shortcut> </Header>
Save the snippet file again.
To test the shortcut, open the project you used previously, type sqrt in the editor and press Tab (once for Visual Basic, twice for C#).
The snippet code is inserted.
You might want the user to replace parts of a code snippet. For example, you might want the user to replace a variable name with one in their current project.
You can provide two types of replacements: literals and objects. Use the Literal element to identify a replacement for a piece of code that is entirely contained within the snippet but will likely be customized after it's inserted into the code (for example, a string or numeric value). Use the Object element to identify an item that's required by the code snippet but is likely to be defined outside of the snippet itself (for example, an object instance or a control).
To enable the user to easily replace the number to calculate the square root of, modify the Snippet element of the SquareRoot.snippet file as follows:
<Snippet> <Code Language="CSharp"> <![CDATA[double root = Math.Sqrt($Number$);]]> </Code> <Declarations> <Literal> <ID>Number</ID> <ToolTip>Choose the number you want the square root of.</ToolTip> <Default>16</Default> </Literal> </Declarations> </Snippet>
Notice that the literal replacement is given an ID (
Number). That ID is referenced from within the code snippet by surrounding it with
<![CDATA[double root = Math.Sqrt($Number$);]]>
Save the snippet file.
Open a project and insert the snippet.
The code snippet is inserted and the editable literal is highlighted for replacement. Hover over the replacement parameter to see the tooltip for the value.
If there's more than one replaceable parameter in a snippet, you can press Tab to navigate from one to the other to change the values.
Import a namespace
You can use a code snippet to add a
using directive (C#) or
Imports statement (Visual Basic) by including the Imports element. For .NET Framework projects, you can also add a reference to the project by using the References element.
The following XML shows a code snippet that uses the method
File.Exists in the System.IO namespace and, therefore, defines the Imports element to import the System.IO namespace.
<?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title>File Exists</Title> <Shortcut>exists</Shortcut> </Header> <Snippet> <Code Language="CSharp"> <![CDATA[var exists = File.Exists("C:\\Temp\\Notes.txt");]]> </Code> <Imports> <Import> <Namespace>System.IO</Namespace> </Import> </Imports> </Snippet> </CodeSnippet> </CodeSnippets>
- Visual Studio: Code snippets schema reference
- VS Code: Create a snippet
Submit and view feedback for