0 Comments

Just a reminder: Visual Studio contains code snippets for many different “languages”, like CSS, HTML, JavaScript, C# etc.

When you want to write a for loop in JavaScript, just hit CTRL + SPACE, then type for, then press the TAB twice and a for loop will be inserted:

 

Before

image

CTRL + SPACE

image

for

image

TAB + TAB

image

 

Edit

This snippet can be found in the folder:

C:\Program Files (x86)\Microsoft Visual Studio 12.0\\JavaScript\Snippets\1033\JavaScript

image

For example, by editing the for.snippet you can change it, so the generated for loop adheres to the JSHint rules:

(note: open Notepad.exe as administrator, else the snippet files can’t be edited.)

Now I changed the for.snippet to:

<CodeSnippet Format="1.1.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>for</Title>
    <Author>Microsoft Corporation</Author>
    <Shortcut>for</Shortcut>
    <Description>Code snippet for a for loop</Description>
    <SnippetTypes>
      <SnippetType>Expansion</SnippetType>
      <SnippetType>SurroundsWith</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>index</ID>
        <ToolTip>Index</ToolTip>
        <Default>i</Default>
      </Literal>
      <Literal>
        <ID>data</ID>
        <ToolTip>Array object</ToolTip>
        <Default>data</Default>
      </Literal>
    </Declarations>
    <Code Language="JavaScript"><![CDATA[for (var $index$ = 0, len = $data$.length; $index$ < len; $index$+=1) {
    $selected$$end$
}]]></Code>
  </Snippet>
</CodeSnippet>

 

Now when I insert the for snippet the following code is generated:

image

 

Manage

To manage Code Snippets just hit CTRL + K, CTRL + B in Visual Studio or menu > TOOLS > Code Snippets Manager…

image

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts