Skip to content

Commit 4889892

Browse files
authored
Merge pull request #3882 from MicrosoftDocs/crdun-publicissue-1497
[mac] improve editor snippets doc
2 parents 1bed2be + deb06e8 commit 4889892

7 files changed

+54
-6
lines changed

mac/media/source-editor-image12.png

-53.1 KB
Loading

mac/media/source-editor-image12a.png

10.8 KB
Loading

mac/media/source-editor-image12b.png

22 KB
Loading

mac/media/source-editor-image12c.png

9.41 KB
Loading

mac/media/source-editor-image12z.png

91.3 KB
Loading
23.1 KB
Loading

mac/snippets.md

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "Code Snippets"
33
description: "How to use code snippets to program efficiently in Visual Studio for Mac"
44
author: conceptdev
55
ms.author: crdun
6-
ms.date: 05/06/2018
6+
ms.date: 02/07/2019
77
ms.assetid: 0FE27C0C-A861-4133-A74E-8D0505CF5342
88
---
99

@@ -17,15 +17,15 @@ This section explains how to create, insert, and use snippets in code.
1717

1818
There are some different ways to add code snippets, some of which are described below:
1919

20-
* **Tab Expansion** - Start typing the template name, select it from the list and press **TAb**, **Tab** to add it:
20+
- **Tab Expansion** – Start typing the template name, select it from the list and press **Tab**, **Tab** to add it:
2121

2222
![Tab Expansion in Code](media/source-editor-image13.png)
2323

24-
* **Toolbox** - Use the toolbox pad to display a list of all code snippets. Drag any template from the toolbox into the correct position in the source code:
24+
- **Toolbox** – Use the toolbox pad to display a list of all code snippets. Drag any template from the toolbox into the correct position in the source code:
2525

26-
![Code snippets in Toolbox](media/source-editor-image14.png)
26+
[![Code snippets in Toolbox](media/source-editor-image14-sml.png)](media/source-editor-image14.png#lightbox)
2727

28-
* **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**:
28+
- **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**:
2929

3030
![Inset Template command](media/source-editor-image15.png)
3131

@@ -35,6 +35,54 @@ While there are many existing templates in a variety of languages that you can u
3535

3636
![Inset new template](media/source-editor-image12.png)
3737

38+
Press the **Add** or **Edit** buttons to create or edit snippets.
39+
40+
## Keywords in code snippets
41+
42+
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.
43+
44+
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:
45+
46+
![Edit template window](media/source-editor-image12z.png)
47+
48+
The following fields are used to define a snippet:
49+
50+
- **Shortcut** – The text the user types to insert the snippet.
51+
- **Group** – Snippets are grouped together in the snippet content menu, using this value.
52+
- **Description** – Explanation of the snippet's purpose.
53+
- **Mime** – Controls what file types the snippet is available in.
54+
- **Is expandable template** – Ensure this is checked so that the snippet can be inserted at the cursor by typing the shortcut.
55+
- **Is surround with template** – Check this option to list this shortcut in the **Surround with...** content menu in the editor.
56+
- **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$`.
57+
- **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.
58+
59+
## Using keywords in the editor
60+
61+
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:
62+
63+
![Inserted snippet showing keywords](media/source-editor-image12a.png)
64+
65+
Press the **Tab** key to move between `object` and `MyProperty` to customize the snippet for your class.
66+
67+
A keyword can be repeated in a snippet, such as this `for` example, notice the `$i$` keyword appears 3 times:
68+
69+
![Snippet template with repeated keywords](media/source-editor-image12b.png)
70+
71+
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:
72+
73+
![Inserted snippet showing multiple keywords](media/source-editor-image12c.png)
74+
75+
### Reserved keywords
76+
77+
There are two reserved keywords that you can use in a snippet:
78+
79+
- `$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.
80+
- `$end$` – When the user has finished editing the keywords in a snippet, the cursor will be placed at the location of the `$end$` keyword.
81+
82+
The `for` snippet in the previous section is an example of both these reserved keywords.
83+
84+
Refer to the [Visual Studio code snippets reference](/visualstudio/ide/code-snippets-schema-reference#keywords) for more information.
85+
3886
## See also
3987

40-
* [Code snippets (Visual Studio on Windows)](/visualstudio/ide/code-snippets)
88+
- [Code snippets (Visual Studio on Windows)](/visualstudio/ide/code-snippets)

0 commit comments

Comments
 (0)