One of the neat new features in Visual Studio 2005 is expansions which assist you in generating code by inserting templates which you can fill in quickly using less keystrokes than would typically be required. The way they work is that you type a well known  snippet name into the code base – snippets integrate with intellisense so the IDE prompts you to a certain extent (you can also press CTRL-K, CTRL-X with C# IDE settings to get a list of expansions).

Prop1

Once you find the expansion you want you just hit TAB-TAB and it inserts the template for you to fill in. You navigate around the template filling in the fields using the TAB key and simply typing in the next that you want.

Prop2

You can see from the screenshot above that this expansion puts in a simple property – this is a killer feature, how much time do you waste defining properties, even if you are like lightning at the keyboard. The best thing is that you can create your own or customise existing ones. Expansions are language specific, so, if your poison is C# then you can look in this directory:

  • C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C#

Alternatively, if you prefer VB.NET, check out this path:

  • C:\Program Files\Microsoft Visual Studio 8\Vb\Snippets\1033

If you open either of those directories you will see a number of *.snippet files. These are XML files which use mark up the snippet to support the TAB navigation (among other things). The mark-up is pretty straight forward. One of my first additions to snippets was my own “prop” expansion, “mdprop”.

This does a few things – first, it stops you having to define the field name and property name seperately (there is only one thing to type in), it prepends a “m_” wart onto the field – and prefixes the field access in the get/set accessors with “this.”. If this is the style you like to use then you can download the expansion here.

Prop3

Another cool thing about expensions is that you don’t need to restart the IDE to use them – it picks up new ones, and modifications to existing ones immediately.

Prop4

There – that fits in with my coding style a little better. And I guess thats the point. Expansions/snippets are all about matching your personal coding style. If what comes out of the box doesn’t match the way you code add or modifiy the set – although I would recommend keeping a back up so you can copy them back in when you rebuild your machine.

5 Responses to “Fine Tuning Expansions in VS2005”

  1. David Smith Says:

    Extremely useful. Productivity++.

  2. John Lewin Says:

    It’s great that this feature is easy to extend. One of my least favorite tasks in writing HTML is wrapping existing content with block tags. For example begin by typing <div>, then delete or cut the </div> the IDE inserts automatically, then page to the end of the section and paste or retype the closing div. Thus, how can I modify or extend the “Wrap with” feature in the IDE? I figure a small extension would allow for any HTML tag to be typed and then wrap the selected text.

  3. Mitch Denny Says:

    It doesn’t seem to be supported for HTML unfortunately.

  4. Will Says:

    The mdprop link is dead! I’ve really been looking for that kind of snippet

  5. Mitch Denny Says:

    Hi Will,

    It should be fixed now.


Leave a Reply