51

I've made a custom snippet for use in Visual Studio. In VS2013, it worked as expected, but since using it in VS2015 (Community Edition) it's been inserting an extra newline before the code (right when I press tab/enter the second time).

This seems to only be a problem with the custom snippet (the built-in ones work fine). Anyone know what could be causing this? It's very annoying.

As a side note, this only happens if I'm activating the snippet on an empty line of code. If I do it after existing code, the newline isn't inserted. Unfortunately, the snippet is a statement, so this doesn't help much.

Here's the snippet, copied almost entirely from the VS sample:

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

    <!-- The header contains information describing the snippet. -->
    <Header>

      <!-- The Title of the snippet, this will be shown in the snippets manager. -->
      <Title>Insert Field Add</Title>

      <!-- The description of the snippet. -->
      <Description>Inserts a basic field add for a DataObject</Description>

      <!-- The author of the snippet. -->
      <Author>Thomas Price</Author>

      <!-- The set of characters that must be keyed in to insert the snippet. -->
      <Shortcut>fadd</Shortcut>

      <!-- The set of snippet types we're dealing with - either Expansion or -->
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>          

    </Header>

    <!-- Now we have the snippet itself. -->
    <Snippet>
        <!-- Create any declarations that we use in the snippet. -->
        <Declarations>
          <Literal>
            <ID>FieldName</ID>
            <ToolTip>Enter the field name</ToolTip>
            <Default>field</Default>
          </Literal>
        </Declarations>

        <!-- Sepecify the code language and the actual snippet content. -->
        <Code Language="CSharp" Kind="any">
            <![CDATA[$FieldName$ = fields.add($FieldName$, "$FieldName$");]]>
        </Code>
    </Snippet>
</CodeSnippet>

thomas88wp
  • 2,381
  • 19
  • 31
  • Possible duplicate of [Visual Studio code snippets extra line](https://stackoverflow.com/questions/33297458/visual-studio-code-snippets-extra-line) – rory.ap Jun 05 '17 at 14:38

1 Answers1

85

You can prevent the preceding newline by placing $end$ somewhere in your snippet text. Example:

<![CDATA[$FieldName$ = fields.add($FieldName$, "$FieldName$");$end$]]>
Craig A
  • 1,368
  • 12
  • 11
  • 4
    is there a `$start$` too? – J86 Jul 22 '16 at 21:21
  • @Ciwan apparently no. I tried it and it didn't make a difference on my snippets – Nick Jul 29 '17 at 02:53
  • 3
    Here's the [full specification reference](https://learn.microsoft.com/en-us/visualstudio/ide/code-snippets-schema-reference#code). There are only "two reserved words available for use in the text of the `Code` element: `$end$` and `$selected$`." @Ciwan – Draco18s no longer trusts SE Oct 09 '17 at 17:19
  • 1
    This may seem obvious, but for this to work you must not add a newline after the `CDATA[` bracket, i.e., you have to start your snippet right after it. I had a multi-line snippet, on which it's natural to want a newline after the `CDATA[`, and since I had tried removing the newline w/o any effect before, it took me a bit to realize you need both things (`$end$` *and* no newline). – Paul Jul 17 '19 at 22:03
  • 1
    This is because by telling VS where it should place the cursor after the snippet is inserted, you prevent the cursor before the inserted code. – Karol Skrobot Apr 11 '20 at 22:00
  • If you put the $end$ in the right place, it will also prevent a newline at the start, and as @Paul said, the statement should start on the same row immediately after the `CDATA[` – Mayer Spitz Jul 14 '20 at 17:35
  • 3
    Had the same issue with VS2019. Adding `$end$` at the end worked for me. – Mustafa Sep 04 '20 at 07:41