Is there any way to insert pre-defined code snippets in Visual Studio?

2

I wonder if Visual Studio makes it possible to manage code snippets for easy insertion into files during an edit. Or if there is some add-on it does.

Example:

I have this piece of code I always use:

<script src="jquery.js" ></script>    

The idea would be to write this snippet of code and insert it easily whenever necessary with few clicks.

    
asked by anonymous 04.08.2017 / 21:33

2 answers

4

Yes, it is possible!

Visual Studio allows you to create custom code snippets. For this it is necessary to create an XML file using the pre-defined structure of Visual Studio.

See full details on MSDN.

Here's a quick step-by-step look at how to create a custom code snippet .

  • Open the Tools menu and enter Code Snippet Manager or go directly to the manager Shortcut Ctrl B p>

  • Create an XML file with this structure

       
    <?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>  
    
  • In attribute Language of tag Code put language

  • Save the file with the extension CDATA

  • Open the manager (from step 1) and click Import

  • 04.08.2017 / 21:49
    3

    Yes, it is possible. This is called Code Snippet and has extensive documentation .

    If this is not enough there are plugins in VS marketplace .

        
    04.08.2017 / 21:45