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

When using code snippets in Visual Studio that contain multiple lines, the following lines retain the spaces that were set in the .snippet file, rather than positioning the code relative to the original cursor location.

When using the foreach snippet, you will get the code as follows:

foreach (var item in collection) { } 

Instead:

  foreach (var item in collection) { } 

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

+6
visual-studio code-snippets
source share
2 answers

Part of the code for the fragment file is contained in CDATA, which preserves spaces. The best I can tell you is to enter the file and edit it according to your needs. The only other option is to make fast Ctrl + K and Ctrl + D after using the fragment to automatically format the code, which will correct the indent.

+3
source share

Edit snippit files:

  • Open a text editor such as notepad as administrator
  • Open the fragment file that you want to fix, for example, foreach.snippet. (Located in C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ VC # \ Snippets \ 1033 \ Visual C # on my computer)
  • Remove the leading tabs of the code element. Therefore, change the Code ... xml element:

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

in

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

All Articles