What’s cool about Code Snippets?

Code snippets are pre-created pieces of code, and Visual Studio supports two kinds of them. Expanding code snippets and surrounding code snippets. You can get started with code snippets right away, since Visual Studio ships with lots of them.

So, how do they work? Well, the expanding code snippets are the easiest to use; you select a snippet by typing in a couple of letters in the editor, and press TAB once or twice to get the code.

You could try typing “cw” and press TAB twice when you’re inside a method, and it will expand to “Console.WriteLine();”. It’ll even place the caret inside the paranthesis for you. You could optionally get to the snippets list with CTRL+K, CTRL+X. (Its a chord, meaning you have to press and keep CTRL pressed while typing the letter K and X in sequence.)

Did you notice the snippet icon icon in the intellisense list? This thorn-in-half code file represents a snippet. Have a look in the intellisense list, and you’ll see this icon alongside many usefull snippets there. Some snippets I use:

  • “cw” for “Console.WriteLine();”. Very usefull when working with console apps.
  • “ctor” for creating a constructor.
  • “for” and “forr” to create a for loop. (“forr” creates a reverse one.)
  • “foreach” to create a foreach loop.
  • “Exception” to create the full code of a custom exception class.
  • …and so on.

When using the snippet, some of them will have special placeholders to replace parts within: Like when using the “for” snippet, you can name the counter-variable. When this snippet is expanded, the i variable is selected in the declaration, and also the “i” variable in the condition and in the iterator parts are dotted. Changing the “i” to something else, will also change the two others.

I’ve gotten so used to snippets that I even use the really simple ones, like “else” (TAB, TAB) to create only an empty else block with the curly braces or “using”.

But there are two more really cool things you should know about snippets. How do surrounding snippets work, and how do I create my own snippets.

First things first: Here’s how you use  a surrounding code snippet:

  1. Select a block of code to place inside the snippet
  2. CTRL+K, CTRL+S or select “Surround with…” from the right-click popup menu.
  3. Select the snippet you want from the list. The list now onsly shows the surrounding snippets. You can start typing to navigate in the list, so there is still no need to use the mouse…

You’ll find that “for”, “forr”, “foreach” and “using” are also surrounding snippets. Others I use a lot are:

  • “#region” to create a region block.
  • “try” and “tryf” for a try-catch block or a try-catch-finally block

Now, is this helpfull? I find it to be, and I hope you do too…

The final thing you’ll want to know is how to create code snippets. I’ll write a separate post on that later!

Snippets are in VS2008 available for C#, XML and VB.NET files. In VS2010 that support extends to ASP.NET markup also.

Leave a Reply

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