I <3 Steve McConnell*
Coding Horror
programming and human factors
by Jeff Atwood

October 16, 2005

Code Snippets in VS.NET 2005

One of the most enjoyable new features in Visual Studio .NET 2005 is Code Snippets. This animated GIF illustrates how it works:

VS.NET 2005 code snippets in action

I'm demonstrating three types of snippets here:

  • simple expansion
  • template expansion (with variables)
  • surround

The easiest way to enter a code snippet is to begin typing part of the snippet shortcut name, eg "prop" for property expansion. Once you've typed enough to uniquely select the snippet shortcut name, press:

Tab,Tab (in C#)

?,Tab (in VB.NET)

.. and the snippet will appear. If it's a templated snippet:

Tab advances to the next field

Shift+Tab moves to the previous field

Enter completes the snippet and resumes editing at your previous cursor position

If you want to enclose code in a surrounding snippet (eg, you have code highlighted for a region), you must manually invoke snippet intellisense by pressing:

Ctrl+K, Ctrl+X

VB.NET's implementation of snippets is a bit more robust than the one in C#.

  1. VB.NET has a zillion snippets shipping in the box where C# has maybe.. two dozen?
  2. VB.NET supports code snippets that automatically add any necessary Import statements.
The VB IDE team has a helpful code snippets FAQ that covers the language differences in detail.

There's a snippet management UI you can invoke via the Tools, Code Snippets Management menu, or by pressing:

Ctrl+K, Ctrl+B

From here you can import and export snippet files, but there's no editor for creating new ones. Snippets are just XML files with a ".snippet" extension that live in these language specific folders:

  • c:\Program Files\Microsoft Visual Studio 8\VB\Snippets\1033
  • c:\Program Files\Microsoft Visual Studio 8\VC#\snippets\1033
Assuming you don't enjoy editing raw XML files, it might be easier to go with Microsoft's Snippy, a GUI for creating VB.NET and C# code snippets. There's also Michael Palermo's new website for sharing code snippets, although there don't seem to be many there at the moment.

Posted by Jeff Atwood    View blog reactions

 

« Media Center 2005 adds Non-Linear Stretch Unit Testing vs. Beta Testing »

 

Comments

So finally, it's able to do the same I'm used to Eclipse for years (and was wondering, why it doesn't exist in Visual Studio .NET 2003).

It's nice but I never learned the shortcuts for complete blocks of code (like 'svm' for static void main).

Also useful (and I assume it exist in VS .NET 2005 as well) is the ability to comment out selected lines with pressing CTRL-SHIFT-7) and uncommenting it again with pressing the same combination.


Best regards, Lothar

Lothar on October 17, 2005 04:34 AM

Yeah, that's...

CTRL+K, CTRL+C (comment)
CTRL+K, CTRL+U (uncomment)

Jeff Atwood on October 17, 2005 05:33 AM

I first encountered snippets in Homesite. I'm glad to see that VS.NET has joined the ranks of editors I would consider using.

I don't think I've ever thought to share my snippets with anyone, though. To me, code snippets are very personal. They're tuned to what my brain thinks is a block of recurring code. The shortcuts are easy to remember because I thunk 'em up myself.

Patrick McElhaney on October 17, 2005 08:28 AM

A better place to put your own snippets is in the folder provided in your My Documents\Visual Studio 2005\Code Snippets folder. It will save you time when you want to export them. Although, if you edit the ones provided by VS, you'd have to move them too, if you want to keep them.

I really like the surround with for XML files. I'm always forgetting elements and the surround with makes it way easier.

btw, there is a shortcut key (Shift+Alt+F10) to include using statements for assemblies. A smart tag shows up at the end of the class name too, if the IDE can find the class.

Jason on October 17, 2005 10:41 AM

Oops. That btw in my last comment was for C#.

Jason on October 17, 2005 10:42 AM

I've been using Code Snippets via Resharper, so I'm looking forward to this being built in.

For example, I created a "Property" snippet so that when you name the private member "_someMember", it automatically names the property "SomeMember". (I noticed you're an underscorer too). Just a bit less typing than the demo you showed.

Haacked on October 17, 2005 11:32 AM

> Shift+Alt+F10

I was experimenting with that when renaming variables, I didn't realize it worked for adding assembly refs as well. I wonder why they made it such a contortion-inducing shortcut chord.. not exactly easy to press.

This also works in VB.NET, but it changes the ref to "Net.WebClient" instead of an "Imports System.Net" statement .. weird.

Jeff Atwood on October 17, 2005 12:45 PM

It's nice to see Microsoft adopting features other IDEs have had for years.

Borland's Delphi has had it for years via the CodeRush add-in. It will be standard in Delphi 2006 (Borland Developer Studio). Borland calls it Live Templates.

I am sure when Microsoft finally delivers a live two-way modeling tool and object persistent framework, we'll hear praise of how ingenious they are. In the meantime, I can continuing using Delphi and get those features /now/.

Kyle A. Miller on October 17, 2005 01:54 PM

> has had it for years via the CodeRush add-in

Well, yeah, but everyone has had everything for years via "add-ins". It just depends how obscure you want to get.

However, there's a huge practical difference between add-in and "included in the box".

I'm not saying it's *better* than the third party solutions, mind you, but.. it's in the box. It'll get used by several orders of magnitude more people.

Jeff Atwood on October 17, 2005 02:21 PM

How did you make that animated GIF?

Mike on October 17, 2005 09:51 PM

I use gif-gif-gif.

http://www.peda.com/ggg/

It's so ancient I think it was written in hieroglyphics, but it still works like a champ for 256-color friendly captures..

Jeff Atwood on October 18, 2005 12:08 AM

Another one of the great features MS Visual FoxPro had for years. Remember to thank YAG and his team for showing the VS.Net interface guys how to do things right in VS.Net 2005.

Alex Feldstein on October 18, 2005 09:22 AM

It's so ancient I think it was written in hieroglyphics, but it still works like a champ for 256-color friendly captures..

shoma on November 9, 2007 07:37 PM

Does anyone know of a snippet cheat sheet that I could post on my wall?

Chris Blankenship on November 27, 2007 01:24 PM

Chris - Cheat Sheet: http://john-sheehan.com/blog/wp-content/uploads/vs2005-code-snippets.pdf

I <3 Code Snippets =D

Rob on February 18, 2008 04:34 AM







(hear it spoken)


(no HTML)




Content (c) 2008 Jeff Atwood. Logo image used with permission of the author. (c) 1993 Steven C. McConnell. All Rights Reserved.