Visual Studio code snippets Cursor

Visual StudioCode Snippets

Visual Studio Problem Overview


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

Visual Studio Solutions


Solution 1 - Visual Studio

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStormenetView Question on Stackoverflow
Solution 1 - Visual StudioHenrik SöderlundView Answer on Stackoverflow