Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio code snippets Cursor

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             }); 
like image 392
Stormenet Avatar asked Jan 20 '10 09:01

Stormenet


People also ask

How do I get rid of the cursor in VS Code?

You can also press ctrl + shift + I . In mac os it is control.

How do I use code snippets in Visual Studio Code?

To create or edit your own snippets, select User Snippets under File > Preferences (Code > Preferences on macOS), and then select the language (by language identifier) for which the snippets should appear, or the New Global Snippets file option if they should appear for all languages.

How do I get multiple cursors in VS Code?

VS Code supports multiple cursors for fast simultaneous edits. You can add secondary cursors (rendered thinner) with Alt+Click. Each cursor operates independently based on the context it sits in. A common way to add more cursors is with Shift+Alt+Down or Shift+Alt+Up that insert cursors below or above.


1 Answers

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> 
like image 195
Henrik Söderlund Avatar answered Sep 21 '22 15:09

Henrik Söderlund