-->
Code snippets, often referred to as code templates, are useful for efficient programming as they allow the insertion and editing of pre-written blocks of code. Using code snippets can be convenient for quickly adding common patterns, or even for learning new patterns when as the developer you are unsure of syntax. There are templates provided for C#, F#, HTML, XML, Python, and Razor.
Snippets make reusing text simple. Just select text – a few words, a whole paragraph, or an entire document – and then create a snippet with one click. Insert snippets with a click or autocomplete as you type. Easily see how text differs from a snippet. Instantly create a new document from snippets. Demonstrating how to circle a word in Microsoft Word.
This section explains how to create, insert, and use snippets in code.
Inserting a snippet
There are some different ways to add code snippets, some of which are described below:
Tab Expansion – Start typing the template name, select it from the list and press Tab, Tab to add it:
Toolbox – Use the Toolbox Window to display a list of all code snippets. Drag any template from the toolbox into the correct position in the source code:
Insert Templates command – There is currently no default key binding set for inserting a template. To create one, browse to Visual Studio > Preferences > Key Bindings and search for
template
. This allows adding the desired key binding into the Edit Binding field, then click Apply:
Creating a new template
While there are many existing templates in a variety of languages that you can use and edit, new templates can also be added by navigating to Visual Studio > Preferences > Text Editor > Code Snippets:
Press the Add or Edit buttons to create or edit snippets.
Keywords in code snippets
After a code snippet is inserted into the editor, any keywords defined are highlighted and can be edited by tabbing between them. Keywords behave like a 'variable' in the code snippet and are defined by placing a dollar-sign $
before and after the name of the keyword.
The Edit template window is shown below, editing the built-in prop
snippet. The snippet contains two keywords – $type$
and $name$
– which can have further properties set (such as a default value and tooltip) on the right side of the window:
The following fields are used to define a snippet:
- Shortcut – The text the user types to insert the snippet.
- Group – Snippets are grouped together in the snippet content menu, using this value.
- Description – Explanation of the snippet's purpose.
- Mime – Controls what file types the snippet is available in.
- Is expandable template – Ensure this is checked so that the snippet can be inserted at the cursor by typing the shortcut.
- Is surround with template – Check this option to list this shortcut in the Surround with... content menu in the editor.
- Template text – The actual snippet that will be inserted into the editor. Keyword placeholders can be defined by surrounding a token with dollar signs eg.
$type$
. - Keyword property panel – On the right-side of the window, use the drop-down list at the top to choose a keyword (eg
type
) and edit properties like default value and tooltip.
Using keywords in the editor
To use a snippet with keywords, such as the one defined above, type the shortcut and press Tab twice, and the snippet contents will be inserted at the cursor:
Press the Tab key to move between object
and MyProperty
to customize the snippet for your class.
A keyword can be repeated in a snippet, such as this for
example, notice the $i$
keyword appears 3 times:
When used in the editor, the Tab key will switch between the first i
and max
. If you overtype the i
with a different variable name, all three instances will be updated:
Reserved keywords
There are two reserved keywords that you can use in a snippet:
$selected$
– If the snippet has Is surround with template checked, this keyword will be replaced by the text that was highlighted in the editor when the snippet was chosen.$end$
– When the user has finished editing the keywords in a snippet, the cursor will be placed at the location of the$end$
keyword.
The for
snippet in the previous section is an example of both these reserved keywords.
See also
Microsoft Word is a very versatile software. It has a large number of great features which the common users don’t even know about. Let’s consider a scenario. The other day, Rajesh, a friend of mine sent me an email and told me that he was preparing an MS Word document and needed to embed programming code with syntax highlight. To insert code snippet in his MS Word document, Rajesh did all the syntax highlighting manually. You can imagine that it is a tedious job! So, he asked me if there was an easier way. Well, indeed there is one!
It is easy to embed code snippets with automatic syntax highlight in MS Word. Let’s learn how you can do this magic!
A code snippet is a piece of computer programming code. Such code is usually marked with several colors in order to show various elements of the code distinctively. For example, all the programming language keywords will be highlighted in a particular color and all the errors will be highlighted in a different color. Similarly, there would be per-defined colors for text, tags, properties, attributes, values, symbols etc.
Our tips on MS-Word make your life easier and increase your productivity at work.
Program lines in a code snippets are also usually indented to make the code easily readable.
First of all, open the MS Word document in which you want to insert the code snippet. Then place the cursor where you want the snippet to be inserted.
Now, go to Insert tab and then click on the Object option. A new box will appear with a list of objects that you can insert in your MS Word document. Select OpenDocument Text from the list and click OK to insert the object.
You will see a new shaded area is inserted at the cursor position. Also a new Word document will open up. Whatever you will write or paste in this new document, the same will appear in the original document.
To add code snippets with syntax highlight, you can use Insert Object method of MS Word.
So, now, copy the code from your code editor and paste it in the new document. Save the new document and that’s it! Code snippet will appear neatly embedded in your MS Word document.
If you have copied the code from a Microsoft product (like MS Visual Studio), the syntax highlight will automatically carry to the MS Word.
However, if you’re using some other code editors (like Programmer’s Notepad), copied code may appear without syntax highlight in MS Word. But don’t panic!
Most of the code editors will give you the option of copying text with HTML markup or RTF format. You should use these options while copying text.
In Programmer’s Notepad, this option appears as Edit -> Copy as RTF
In Notepad++ the option is available as Plugins -> Copy Text with Syntax Highlighting
If you’re copying syntax highlighted code from a webpage, the HTML markups will automatically get copied and syntax highlight will carry to MS Word.
Microsoft Word For Mac
Although you can always manually highlight various portions of a code snippet in MS Word, but there are many distinctive advantage of using the embedded object method:
- Code and main document remain separate from each other (programmers love this approach, don’t they?)
- Spelling and grammar mistakes will not be highlighted by MS Word in the embedded code snippet. It is of great help because a lot of programming keywords are not found in dictionary, and as a result such code may result in lots of red and green wavy lines.
- And obviously, it’s easier, quicker method. Code remains in it’s original format and syntax highlights.
- Formatting changes in the main document do not affect the code block.
Microsoft Word For Mac Download
So, did you find this MS Word tips useful? Whenever you would need to insert a code snippet with syntax highlight in MS Word document, I am sure it will save you a lot of time. Please feel free to ask any questions on this tutorial. I will try my best to assist you. Thank you for using TechWelkin.