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
            });

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

6 Comments

I'm guessing that maybe the $end$ thingy may be what you are looking for.
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.
Yep, that did the trick. And I'm not surrounding code, so that's no issue for me. Thanks!
@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.
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
|

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.