Saturday, September 27, 2008

Sara Ford points to the release of Snippet Designer by Matthew Manela.

Snippets are a remarkably cool productivity tool within Visual Studio. I say "remarkable", because many people don't even know they exist!

It's great to have a UI over the top of the snippet XML files, which are a little painful to edit by hand. Snippet Designer lets you select a section of code, and export it to the designer to create a snippet:

image

(I had to cheat to get the screenshot of the menu... anyone know how to do that?)

Then you're dropped into the Snippet Explorer's designer surface, where you have three or four simple steps:

image

  1. Right-click a symbol and create a replacement (above, I turned the 'argument' variable into a replacement). A replacement is text that the user can edit when using your snippet.
  2. Edit the details of the replacement, including the default value and tooltip text.
  3. Add a shortcut to your snippet.
  4. (Optionally) Enter $selected$ $end$ where you'd like the cursor to appear after the snippet is finished. This is imported for SurroundsWith snippets, as the text you're surrounding will be replaced here.

Then simply save the snippet into your snippets directory inside My Documents (the default location is probably right), and now you can use your snippet when editing code.

Type the shortcut string (mine was 'fow'):

image

Press "Tab" "Tab":

image

And you end up in the first replacement variable, with the default value entered. Over-write the text with what it should be, and press Tab to jump to the next replacement. Continue pressing tab until everything is correct, and press Enter. You'll jump to the middle of the replacement text:

image

The snippet file is just XML, so you can edit it directly, or copy it to other computers easily.

There are a lot of snippets installed out of the box, you can browse them using the old Code Snippets Manager (under the Tools menu), or using the new Snippets Explorer installed by the Snippets Designer (under View -> Other Windows).

I'll be showing off the Snippets support in future Visual Studio tips n tricks talks. If you're interested in more tips, I have a Visual Studio 2008 series on this blog, or you can visit Sara Ford's blog.

Previous:

Saturday, September 27, 2008 7:19:54 PM (New Zealand Standard Time, UTC+12:00)
Nice. Now that I've run out of disk space for virtual machines, snippets and templates are my new addictions, so anything that makes it easier to create snippets is definitely a Good Thing.

"I had to cheat to get the screenshot of the menu... anyone know how to do that?"

Use PrtSc instead of Alt+PrtSc. You're cropping anyway, so no loss.
Saturday, September 27, 2008 9:03:06 PM (New Zealand Standard Time, UTC+12:00)
Arg. Of course. Cheers :)

Kirk
Tuesday, September 30, 2008 6:02:40 AM (New Zealand Standard Time, UTC+12:00)
Thanks for taking interest in the Snippet Designer. My hope is that by releasing this open source on CodePlex I can get more people looking at it and helping to imrpove the Snippet Experience inside of Visual Studio.

-Matt
Comments are closed.