Visual Studio code snippets Cursor
Asked Answered
P

1

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
            });
Plumage answered 20/1, 2010 at 9:26 Comment(0)
A
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>
Airport answered 20/1, 2010 at 9:44 Comment(6)
I'm guessing that maybe the $end$ thingy may be what you are looking for.Corpuscle
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.Corpuscle
Yep, that did the trick. And I'm not surrounding code, so that's no issue for me. Thanks!Plumage
@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.Toddler
@haagel: That is a nice trick. Annoying with the space though.Corpuscle
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-snippetBonnybonnyclabber

© 2022 - 2024 — McMap. All rights reserved.