61

In Visual Studio when you insert a snippet and finish inserting literals the cursor jumps to the beginning of the snippet.

Now I'd like to tell visual studio where the cursor should go afterwards. I've searched the web and actually hold little hope for this to be possible.

To illustrate, suppose I have this snippet:

<Code Language="csharp" Kind="method body" Delimiter="$"><![CDATA[this.SyncThreadRunInvoke(() =>
            {

            });]]>
    </Code>

Then after inserting:

this.SyncThreadRunInvoke(() =>
            {
            []<- I want the cursor here
            });
Tim Murphy
  • 4,892
  • 4
  • 40
  • 48
Stormenet
  • 25,926
  • 9
  • 53
  • 65

1 Answers1

94

Use the $end$ variable as shown in the following "if" snippet for c#.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>if</Title>
            <Shortcut>if</Shortcut>
            <Description>Code snippet for if statement</Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
                <SnippetType>SurroundsWith</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>expression</ID>
                    <ToolTip>Expression to evaluate</ToolTip>
                    <Default>true</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[if ($expression$)
    {
        $selected$ $end$
    }]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
Philipp M
  • 1,877
  • 7
  • 27
  • 38
Henrik Söderlund
  • 4,286
  • 3
  • 26
  • 26
  • 9
    I'm guessing that maybe the $end$ thingy may be what you are looking for. – Henrik Söderlund Jan 20 '10 at 09:49
  • 1
    Yep, I've tried it now, it is definately $end$ that you want. One little caveat though: If you are creating a snippet that surrounds existing code with new code, such as curly braces or whatever, it seems logical to me that the following would place the cursor *after* the surrounded code: $selection$ $end$ But it does not. It places the cursor immediately *before* the surrounded code. – Henrik Söderlund Jan 20 '10 at 10:01
  • Yep, that did the trick. And I'm not surrounding code, so that's no issue for me. Thanks! – Stormenet Jan 20 '10 at 11:51
  • @Henrik Söderlund - If you write "$selected$$end$", the cursor will be placed immediately before the code just as you say. But if you write "$selected$ $end$", i.e. insert a space between the two, the cursor will end up after the code. I have no idea why, and it creates an annoying space after the code. – haagel Jan 11 '13 at 13:30
  • @haagel: That is a nice trick. Annoying with the space though. – Henrik Söderlund Jan 11 '13 at 20:00
  • 1
    does anyone have any references to documentation on snippets? I'd like to learn more. Also have the following open question [http://stackoverflow.com/questions/21701369/unassigned-shortcut-for-sql-code-snippet](http://stackoverflow.com/questions/21701369/unassigned-shortcut-for-sql-code-snippet) – whytheq Mar 15 '14 at 13:14