ReSharper 8.0 Web Help

Surround templates let you quickly enclose an expression, a single statement or an arbitrary block of code with a predefined code construction, such as for loop, or try...catch statement. Surround templates have no shortcuts, they are identified by their descriptions. See Surrounding Code Fragments with Templates for details.


Your new Surround templates can be readily available to other developers. Depending on your needs, you can create the new code template in different settings layers including team-shared layers. If necessary, you can share it later by copying to a team-shared settings layer or by exporting it into a file. It might be also useful to learn to study the way ReSharper stores templates.

The body of a surround template consists of literal strings and parameters. The idea is to combine these to maximize efficiency when invoking the template.

To create a new surround template
  1. On the main menu, choose ReSharper | Templates Explorer.
  2. In the Templates Explorer window that appears, click the Surround Templates tab.
  3. Optionally, if you want to create your template in a specific settings layer, choose the desired layer in the Layer drop-down list. To learn more, see Storage of Templates.
  4. Click New Template iconCreateTemplate. The newly created template is opened in the Template Editor.


    Template Editor opens in a new document tab so that you can switch between the template and the application code to quickly test the created template.

  5. Specify template name in the Description field.


    ReSharper does not save each code template in a separate file. Therefore, the Description identifier does not have to be unique. However, try to provide comprehensible identifiers so as not to be confused when applying your custom code templates.

  6. To change the default scope of the template, click the Availability hyperlink. In the Select scopes dialog box that appears, use check boxes to define where the new template can be applied. You can expand some scopes to specify details (e.g. file masks or language versions):


  7. As soon as the new surround template is opened in the editor, its body contains the $SELECTION$ parameter. This parameter is replaced by the current selection when the template is applied. Make sure to place this parameter in the right place and not to remove it.
  8. Create the body of the template by typing or pasting the desired code.
    The template code can contain plain text and parameters. As soon as you type a parameter (for example, $PARAM$), it appears in the Parameters section to the right of the text area. Configure all defined parameters as described in the Declaring Template Parameters section.
  9. Configure template formatting options:
    • Select the Reformat check box to make ReSharper automatically reformats the expanded code fragment according to your code style settings.
    • Select the Shorten qualified references check box to make ReSharper automatically insert namespace import directives or remove qualifiers that become redundant after the template is applied. If this check box is not selected, ReSharper will not insert any namespace import directives.
  10. To save the template, choose File | Save Selected Items on the Visual Studio menu or press Ctrl+S.


After creating new templates, you may need to organize your templates by categories and/or add it to the quick access list .