Condividi tramite


Using Code Snippets

Keyboard:  CTRL + K, CTRL + X
Menu:  Edit -> IntelliSense -> Insert Snippet
Command:  Edit.InsertSnippet
Versions:  2008,2010
Published:  4/22/2010
Code:  vstipEdit0053

 

Download the seriously cool Tip of the Day Extension to get the daily tips delivered to your Start Page!

 

I thought it would be instructive to demonstrate how to use snippets.  So, let's say you are in an ASP.NET application and you start to type an anchor tag:

image

 

And you hit TAB twice to get this:

image

 

Snippets have special areas that you can change the values in by hitting TAB to cycle through them.  For example, this snippet has two special areas with the first one highlighted.  I'll put in a URL:

image

 

Then hit TAB to go to the next area and type in some text:

image

 

I can keep hitting TAB to go between these two locations as much as I want.  When I am done I can press ENTER and continue typing more code:

image

 

So, in a nutshell, that's how to work with snippets.  They take a little getting used to but are great timesavers.

Comments

  • Anonymous
    April 21, 2010
    For some snippets (at least in VS2008), Tabbing back and forth using TAB doesn't work. Examples are if, for, foreach. Instead, after the last item is reached, pressing TAB jumps to the place where you enter the code. The nice thing with these "special areas" is that other text in the snippet can be linked to them. Ex. in the "for" snippet, changing the index variable also adjusts the other occurences.

  • Anonymous
    October 09, 2010
    yep thats what I came here for... I was hoping the arrow keys would move me to the highlight... but the magic happens with TAB. MUCH LOVE to the VS team! =)