Home / Tips / How to create and use code snippets

How to create and use code snippets

/
/
/
773 Views

In this post we will see how to use the default code snippets and then how to create your own. Don’t know what are code snippets or why to use them? READ OUR TIP about that.

Stage 1

Goal: Use Monodevelop Default Code Snippets

Step 1. Press the Right-Mouse Button to access the context menu and choose Insert Template.

How to use code snippets - Step 1
Press the Right-Mouse Button to access the context menu and choose Insert Template.

Step 2a. To use a snippet from the list just Double Click with the Left-Mouse Button on the keyword. Try with for.

How to use code snippets - Step 2
To use a snippet from the list just Double Click with the Left-Mouse Button on the keyword. Try with for.

Step 2b. (ALTERNATIVE) Or you can just start typing the keyword and press tab to select and tab to insert.

How to use code snippets - Step 3
Or you can just start typing the keyword and press tab to select and tab to insert.

Step 3. Customize the snippet. You can loop through the variables using the tab key and hit enter to finish.

How to use code snippets - Step 4
Customize the snippet. You can loop through the variables using the tab key and hit enter to finish.

Step 4. You can also insert code snippets around existing code. Select all the methods and press the RMB to access the context menu and select Surround With…

How to use code snippets - Step 5
You can also insert code snippets around existing code. Select all the methods and press the RMB to access the context menu and select Surround With…

Step 5. To use a snippet from the list just Double Click with the Left-Mouse Button on the keyword. Try with #region.

How to use code snippets - Step 6
To use a snippet from the list just Double Click with the Left-Mouse Button on the keyword. Try with #region.

Step 6. Customize the snippet. You can loop through the variables using the tab key and hit enter to finish.

How to use code snippets - Step 7
Customize the snippet. You can loop through the variables using the tab key and hit enter to finish.

Stage 2

Goal: Create your own Monodevelop Code Snippet

Step 1. Menu Tools and select Options.

How to create your own code snippet - Step 1
Menu Tools and select Options.

Step 2. Select Code Templates on the Text Editor section and press Add.

How to create your own code snippet - Step 2
Select Code Templates on the Text Editor section and press Add.

Step 3. Add the necessary fields:

  • Shortcut: keyword
  • Description: text that appears on the tooltip next to the keyword on the list
  • Group: you may add your snippet to existing groups or create your own group of snippets (recommended)
  • Mime: the mime type of the snippet you are creating
How to create your own code snippet - Step 3
Add the necessary fields.

Step 4. Let’s try create a snippet for Debug.Log.

How to create your own code snippet - Step 4
Let’s try create a snippet for Debug.Log.

Step 5. On the Template Text you type the code as you would on the editor. If you need to customize the code after insert, you define a variable directly on the template by using $variablename$.

How to create your own code snippet - Step 5
On the Template Text you type the code as you would on the editor.

Step 5. Use it on the editor just as any other code snippet.

How to create your own code snippet - Step 6
Use it on the editor just as any other code snippet.

Step 6. Customize the snippet. You can loop through the variables using the tab key and hit enter to finish.

How to create your own code snippet - Step 7
Customize the snippet. You can loop through the variables using the tab key and hit enter to finish.

Step 8. In this example I created a code snippet to print on the console log the value of a specified variable – $variable$.

How to create your own code snippet - Step 8
In this example I created a code snippet to print on the console log the value of a specified variable – $variable$.

 

If you want to take your snippets with you to another computer you need to copy the snippets file located in [MAC]~/Library/MonoDevelop-Unity-2.8/Snippets/keyword.template.xmlt or [WINDOWS] C:\Users\username\AppData\Roaming\MonoDevelop-Unity-4.0\Snippets

 

  • Facebook
  • Twitter
  • Google+
  • Linkedin
  • Pinterest
  • Reddit

1 Comments

Leave a Comment

Your email address will not be published. Required fields are marked *