2

I am trying to create a Visual Studio snippet and cannot get it to add basically a blank line. See below for my example:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

  <CodeSnippet Format="1.1.0">
    <Header>
      <Title>Bootstrap Row</Title>
      <Shortcut>brow</Shortcut>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="HTML"><![CDATA[<div class="row">
        $selected$$end$
</div>]]>
       </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

What I am trying to get is:

<div class="row">
    CursorHere
</div>

What I actually am getting is:

<div class="row">
CursorHere</div>

Any tips or tricks to get this to do what im trying?

Thanks!

2
  • I've tested your snippet in VS2015. It places /div> on next line for me, but also it erases CursorHere text! It looks like a something is broken in html snippets. Commented Oct 14, 2015 at 18:37
  • cursorHere isnt actually supposed to show up as text just literally place the cursor there which this snippet does just it places </div> where right next to the cursor and ignores that empty line where $end$ is. Its driving me mad! I would like to enter a shortcut click tab and bam start typing the code I want. Right now I am having to place a character there like $selected$$end$b If I have the b there itll do it like I want but just have to either hit backspace or delete to remove the b and then start typing. Commented Oct 14, 2015 at 18:51

2 Answers 2

4

Insert $end$ just before ]] in CDATA block. Full specification: https://learn.microsoft.com/en-us/visualstudio/ide/code-snippets-schema-reference?view=vs-2019#code

By telling VS where it should place the cursor after the snippet is inserted, you prevent the new line and placing the cursor before the inserted code.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is one thing I have tried and it worked but not sure if there is still not a better way out there!

I added basically a literal to the snippet and just gave it a default of "code" so it works how I wanted and I can just begin typing to overwrite what I actually want there.

Here it is:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

  <CodeSnippet Format="1.1.0">
    <Header>
      <Title>Bootstrap Row</Title>
      <Shortcut>brow</Shortcut>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>classes</ID>
          <ToolTip>Add Addition Classes</ToolTip>
          <Default></Default>
        </Literal>

        <Literal>
          <ID>text</ID>
          <ToolTip></ToolTip>
          <Default>code</Default>
        </Literal>
      </Declarations>
      <Code Language="HTML"><![CDATA[<div class="row $classes$">
                                            $selected$$text$$end$
                                    </div>]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Output:

<div class="row ">
    code
</div>

Like I said above code is already selected so you can just start typing without having to delete.

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.