About This
In this blog, I am writing the basics of creating our own snippets in Visual Studio 2010 IDE.
Introduction
Basically, Visual Studio writes some set of code for us by typing shortcuts & pressing TAB.
List of Visual Studio snippet shortcuts:
#if
- Creates an #if
directive and an #endif
directive. #region
- Creates a #region
directive and an #endregion
directive. ~
- Creates a destructor for the containing class. attribute
- Creates a declaration for a class that derives from Attribute
. checked
- Creates a checked block. class
- Creates a class declaration. ctor
- Creates a constructor for the containing class. cw
- Creates a call to WriteLine
. do
- Creates a do while
loop. else
- Creates an else
block. enum
- Creates an enum
declaration. equals
- Creates a method declaration that overrides the Equals
method defined in the Object
class. exception
- Creates a declaration for a class that derives from an exception (Exception
by default). for
- Creates a for
loop. foreach
- Creates a foreach
loop. forr
- Creates a for
loop that decrements the loop variable after each iteration. if
- Creates an if
block. indexer
- Creates an indexer declaration. interface
- Creates an interface declaration. invoke
- Creates a block that safely invokes an event. iterator
- Creates an iterator. iterindex
- Creates a "named
" iterator and indexer pair by using a nested class. lock
- Creates a lock block. mbox
- Creates a call to System.Windows.Forms.MessageBox.Show
. You may need to add a reference to System.Windows.Forms.dll. namespace
- Creates a namespace
declaration. prop
- Creates a property declaration and a backing field. propg
- Creates a property declaration with only a "get
" accessor and a backing field. sim
- Creates a static int Main
method declaration. struct
- Creates a struct
declaration. svm
- Creates a static void Main
method declaration. switch
- Creates a switch
block. try
- Creates a try
-catch
block. tryf
- Creates a try
-finally
block. unchecked
- Creates an unchecked block. unsafe
- Creates an unsafe
block. using
- Creates a using
directive. while
- Creates a while
loop.
Apart from these default snippets, we can create our own code snippets. Generally, we can write custom snippets for repeatable codes across projects.
Creating Custom Snippets
Basically custom snippets save in .snippet file, it is XML based file, and we can manually create this XML file in a snippet acceptable format and add it into Visual Studio IDE. It is very difficult to write in XML.
Open source Snippet Designer available in CodePlex site. It can be downloadable and installed in Visual Studio 2010 IDE. Please see the reference section for download Snippet Designer.
Step 1
Select a set of code wants to create snippets and right client, select Export as Snippet from the popup menu.
It will create a new .snippet file with the selected content.
Step 2
Modify the snippet name and properties as seen below. We can also make modifiable text in the code snippet. Here I have configured arg1
, arg2
as replacement text.
Save this file in the local path with folder name Snippet Demo and named as SnippetAdd.snippet.
Adding Custom Snippet to VS 2010 IDE
Step 1
Open VS 2010 IDE and select tools menu and click Code Snippets Manager item. Code snippet manager appears only if you install Snippet Designer tool.
It will open the Code Snippet Manager window.
Step 2
Click add button and select the folder Snippet Demo, contains SnippetAdd.snippet file.
It will create the folder name Snippet Demo and create a snippet as SnippetAdd
.
Consuming Custom Snippets
Step 1
Type a word SnippetAdd
in the code editor window and presses TAB, it creates the code in the snippetAdd.snippet file.
Here arg1
and arg2
are editable variables and auto replace all other places it referred.
Conclusion
I hope this blog helps you to create the basics of creating our own snippets in Visual Studio 2010.
References
- http://msdn.microsoft.com/en-us/library/ms165392(VS.80).aspx
- http://snippetdesigner.codeplex.com/releases/view/50044