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?
“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.
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.
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.
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.
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.
Edit the snippit files:
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With