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 FilesMicrosoft Visual Studio 8VBSnippets1033
  • c:Program FilesMicrosoft Visual Studio 8VC#snippets1033
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.

Read more

Stay Gold, America

We are at an unprecedented point in American history, and I'm concerned we may lose sight of the American Dream.

By Jeff Atwood · · Comments

The Great Filter Comes For Us All

With a 13 billion year head start on evolution, why haven't any other forms of life in the universe contacted us by now? (Arrival is a fantastic movie. Watch it, but don't stop there - read the Story of Your Life novella it was based on

By Jeff Atwood · · Comments

I Fight For The Users

If you haven't been able to keep up with my blistering pace of one blog post per year, I don't blame you. There's a lot going on right now. It's a busy time. But let's pause and take a moment

By Jeff Atwood · · Comments

The 2030 Self-Driving Car Bet

It's my honor to announce that John Carmack and I have initiated a friendly bet of $10,000* to the 501(c)(3) charity of the winner’s choice: By January 1st, 2030, completely autonomous self-driving cars meeting SAE J3016 level 5 will be commercially available for passenger

By Jeff Atwood · · Comments