Telerik blogs

A few weeks ago I blogged about creating your own reusable templates for Visual Studio. One of my favorite things about Visual Studio is that it offers a lot of great ways for developers to improve the development experience (and productivity) by adding many extensibility points in the IDE. Now developers have the option of adding on to existing features of Visual Studio with templates and macros, or integrating completely unique features directly into the IDE with add-ins. One of these extensibility features, snippets, adds a ton of value to developer productivity and is incredibly easy to use.

Snippets are small, parameterized code templates. They can save developers time by quickly generating repetitive code such as class definitions, property declarations, or iterations. Whereas before Visual Studio we had to drudge through such repetitive programming tasks, now we can add these bits of code to our projects with just a few keystrokes.

Using Snippets

In order to begin using snippets, all you have to do is type the snippet name and it will appear in Visual Studio's intellisense menu.

1

Once you have either fully typed the snippet name or selected it from the intellisense, simply hit TAB,TAB to fully insert the snippet into your code file. For example, typing foreach and then hitting TAB,TAB will insert a parameterized foreach loop into your code.

2

An alternative method for inserting snippets is to select them directly from a snippet menu. Using the shortcut Ctrl-K, Ctrl-X will open the snippet menu. From here developers can navigate through folders of snippets to find the right one to insert into the code.

3

 

When a snippet is inserted into your code, editable field are highlighted and can be navigated to and changed before the snippet is fully inserted. A change to a field will change all other instances of the field in the snippet. You can navigate the snippet code using the following keys:

  • TAB - Navigates to the next field
  • SHIFT + TAB - Navigates to the previous field
  • ENTER - Finishes editing, returns cursor to its position prior to inserting the snippet

Code Snippet Manager

Visual Studio includes a Code Snippets Manager which can be used to browse snippets by language, as well as add, remove, and import snippets. My next post will cover how to add your own custom snippets, but if you'd like to take a look at what snippets are shipped with the IDE, open the Code Snippets Manager by navigating to Tools | Code Snippets Manager... or typing the shortcut CTRL+K,CTRL+B.

4 

If you'd like to get a head start on my next post and see what makes a code snippet, pay attention to the Location text box at the top of the Code Snippets Manager. When you highlight a snippet, Location will show the directory the snippet's XML file is located. Take a peak inside one or two to get a feel for how a snippet is created.


Comments

Comments are disabled in preview mode.