How to create and use code snippets
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.




Step 2a. 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.




Step 3. 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…




Step 5. 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.




Stage 2
Goal: Create your own Monodevelop Code Snippet
Step 1. Menu Tools and select Options.




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




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$.




Step 5. 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.




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




1 Comments
You must log in to post a comment.