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

Asked

Viewed 707 times

2

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

Example:

I have this bit of code that I always use:

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

The idea would be to record this piece of code and insert it easily whenever necessary, with a few clicks.

2 answers

5


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 structure predefined by Visual Studio.

See all the details on MSDN.

Here’s a little step-by-step how to create a snippet custom code.

  1. Open the menu Tools and goes into Code Snippet Manager or access the direct manager via the shortcut Ctrl + K + B

  2. 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>  
    
  3. In the attribute Language tag Code put the language

  4. Inside the brackets of CDATA enter the code of snippet

  5. Save the file with the extension .snippet

  6. Open the manager (from step 1) and click Import

4

Browser other questions tagged

You are not signed in. Login or sign up in order to post.