Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to insert a multi-line code snippet relative to the cursor position in Visual Studio?

When using code snippets in Visual Studio that contain multiple lines the following lines will preserve the whitespace that was set in the .snippet file instead of positioning the code relative to the original cursor placement.

When using the foreach snippet you'll get code like this:

    foreach (var item in collection)
{

}

Instead of:

    foreach (var item in collection)
    {

    }

Is there a way to change this behavior? Is there a keyword that needs to be used in the .snippet file?

like image 435
toluca70 Avatar asked Jan 28 '09 16:01

toluca70


People also ask

How do you move cursor on multiple lines in VS code?

“put cursor on multiple lines vs code” Code Answer's Windows: Ctrl + Alt + Arrow Keys. Linux: Shift + Alt + Arrow Keys. Mac: Opt + Cmd + Arrow Keys.

How do I add multiple lines in Visual Studio?

For Visual Studio Code just hold alt and you can select. Show activity on this post. Show activity on this post. If we type a , the , will be inserted on all the lines simultaneously.

How do I get multiple cursors in Visual Studio?

To add cursors at arbitrary positions, select a position with your mouse and use Alt+Click (Option+Click on macOS). You can add additional cursors to all occurrences of the current selection with Ctrl+Shift+L. Note: You can also change the modifier to Ctrl/Cmd for applying multiple cursors with the editor.

How do I add a code to Snippet in Visual Studio?

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.


2 Answers

The code portion of a snippet file is contained in a CDATA which preserves whitespace. The best thing I can tell you is to go into the file and edit it to suit your needs. Your only other option is to do a quick Ctrl+K and Ctrl+D after you use the snippet to auto-format the code which will fix the indenting.

like image 103
Andrew Hare Avatar answered Oct 20 '22 23:10

Andrew Hare


Edit the snippit files:

  1. Open some text editor, like notepad, as administrator
  2. Open the snippet file you want to fix, e.g., foreach.snippet. (Located in C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C# on my computer)
  3. Remove the leading tabs of the Code Element. So change the Code... xml element from:

            <Code Language="csharp"><![CDATA[foreach ($type$ $identifier$ in $collection$)
            {
                $selected$ $end$
            }]]>
            </Code>
    

to:

            <Code Language="csharp"><![CDATA[foreach ($type$ $identifier$ in $collection$)
{
    $selected$ $end$
}]]>
            </Code>
like image 36
Isak S Avatar answered Oct 20 '22 23:10

Isak S