Code Snippets in VS2005


If you haven’t yet found the Code Snippets feature in VS 2005 it’s time to give it a look.  It’s one of those great features that it’s in VS 2005 but one that gets very little attention from most people because it’s sort of a “bonus feature”.   You don’t it… it’s certainly not a necessity… but if you utilize it I think you’ll find it’s really handy.


 


So, what are Code Snippets?  Basically, a Code Snippet is just that… a snippet of code that VS hangs onto for you which can then be easily inserted into your code through the IDE. 


 


How do they work?  Just right-click in one of your files and click “Insert Snippet”… then drill into the snippet folders and select one of the many built-in snippets that VS ships with.   Then you can quickly tab through the inserted snippet replacing the necessary variables with your own values. 


 


Now, it gets even better… the real power here is that you can write your own snippets and organize them to your liking.   Navigate to [sysdir]:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033 on your local box and you can browse though the various snippet XML files that VS2005 ships with.  Try creating a new directory and adding a few of your own.


 


To install your new snippets follow these steps:


In VS2005 click Tools > Code Snippets Manager > Add.


Navigate to the directory containing your new snippets and select it by clicking the Open button.


You should see a new directory show up in the Code Snippets Manager. 


Close the Code Snippets Manager. 


 


Now, right-click again in your file and select "Insert Snippet", or just type the shortcut that you defined in your snippet definition.  This time, choose one of the new custom snippets you just added.  It’s that easy. 


 


Finally, go to http://msdn.microsoft.com/vstudio/downloads/codesnippets/default.aspx and you can install the various Code Snippets that Microsoft publishes for folks to get their hands on. 


 


I see a ton of great uses for this besides just convenience - on our team we’re going to try to use snippets to document best practices, coding guidelines, etc. 


 


Aaron Bjork


 


 


Comments (2)
  1. Aaron Bjork says:

    Great post Jens.  Thanks.

Comments are closed.

Skip to main content