Write your own Code Snippet for VS
23 Nov 2016Have you ever written bulky codes repeatedly that makes you feel lazy? Have you ever used snippets for fast coding?
Of course it would be a big YES!!. We often write for and the press tab twice to get following code
for (int i = 0; i < length; i++)
{
} Again we use **if** and press **tab** twice to get code as below
if (true)
{
}
Now its time to write our own code snippets. Just follow the instructions given below.
- Create a new file called *.snippet
-
Paste this code
<?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title></Title> <Shortcut></Shortcut> <Description></Description> <Author></Author> </Header> <Snippet> <Code Language="csharp"> <![CDATA[ "code here" ]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets>
Things might get messy here so carefully put the values inside the tags. As mentioned in the header, we have Title, Shortcut, Description and Author. Title : Title of snippet Shortcut: Your shortcut to snippet to double tap on it. Description : Description of your snippet. You can keep this blank. Author: Since you are the creator, of course your name here. Now the main thing comes in Code tag. You can see ![CDATA[ “code here”]]. Write down your code replacing “code here”. It doesn’t matter if it is multi-line, just go through it. Don’t leave inverted comma “ hanging around there.
-
Save it.
- Now open Visual Studio and goto Code Snippets Manager in Tools menu.
- Now goto import and select your code snippet.
- After importing, you are ready to go
- Type your shortcut and double tab on it
Cheers Happy coding