One of the most enjoyable new features in Visual Studio .NET 2005 is Code Snippets. This animated GIF illustrates how it works:
I'm demonstrating three types of snippets here:
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#.
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:
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
Yeah, that's...
CTRL+K, CTRL+C (comment)
CTRL+K, CTRL+U (uncomment)
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 8:28 AMA 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 AMOops. That btw in my last comment was for C#.
Jason on October 17, 2005 10:42 AMI'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 PMIt'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 1: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 2:21 PMHow did you make that animated GIF?
Mike on October 17, 2005 9:51 PMI use gif-gif-gif.
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 AMAnother 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 9:22 AMIt'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 7:37 PMDoes anyone know of a snippet cheat sheet that I could post on my wall?
Chris Blankenship on November 27, 2007 1:24 PMChris - Cheat Sheet: http://john-sheehan.com/blog/wp-content/uploads/vs2005-code-snippets.pdf
I <3 Code Snippets =D
Rob on February 18, 2008 4:34 AMCan someone provide the registration code to the GifgIfgiF program? it would be great help :)
azariel on April 29, 2009 9:11 AM| Content (c) 2009 Jeff Atwood. Logo image used with permission of the author. (c) 1993 Steven C. McConnell. All Rights Reserved. |