vs - how to create a surround-with code snippet

How to create a Visual Studio Surround With code snippet for html:



  hello
  
  
  
[insert-snippet.png]

  <p>
      hello
  </p>
  

Visual Studio
Tools ... Code Snippets Manager ... Import ... [filename.snippet]


The snippet file is copied to your Documents directory:


  C:\Users\Richard\Documents\Visual Studio 2013\Code Snippets\Visual Web Developer\My HTML Snippets\html-para.snippet
  

    Tokens used in this script:
  • $end$ marks where to put the cursor after the snippet has run.
  • $selected$ is the selected text prior to running the snippet.

html-para.snippet


  <?xml version="1.0" encoding="utf-8" ?>
  <CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
      <CodeSnippet Format="1.0.0">
          <Header>
              <Title>p</Title>
              <Shortcut>para</Shortcut> <!-- type para and press TAB to insert snippet -->
              <Description>Insert HTML p element</Description>
              <Author>Zuga.net</Author>
              <SnippetTypes>
                  <SnippetType>Expansion</SnippetType>
                  <SnippetType>SurroundsWith</SnippetType>
              </SnippetTypes>
          </Header>
          <Snippet>
              <Code Language="html"> <!-- begin code here -->
                  <![CDATA[<p>
                  $end$$selected$
                  </p>]]>
              </Code> <!-- end code here -->
          </Snippet>
      </CodeSnippet>
  </CodeSnippets>
  

3 ways to use the code snippet:
  1. Type para (the ShortcutText). Press [TAB]
  2. Control+K+X (Insert Snippet) ... My HTML Snippets ... para. Press [TAB or Enter]
  3. Control+K+S (Surround With) ... My HTML Snippets ... para. Press [TAB or Enter]

Ads by Google



Ask a question, send a comment, or report a problem - click here to contact me.

© Richard McGrath