ReSharper Web Help

Live templates are ReSharper's far more intelligent version of Visual Studio's code snippets. Unlike code snippets, ReSharper can predict from the context which variables, fields, and types can be used in corresponding templates. In most cases you do not need to type anything yourself. Just choose from a list of suggested values.

ReSharper provides more than 20 live templates for C# alone. There are a lot of predefined live templates for VB.NET, ASP.NET, XML, HTML, Razor, CSS, JavaScript, and Resx code files. The list of predefined templates includes templates for iterating through collections, writing assertion methods, creating new arrays, outputting values, coding LINQ queries and generating auto-properties, and more.

To create a code fragment using live template
  1. In the text editor, put the caret where you want to insert a code fragment from live template.
  2. Do one of the following:
    • Type a template shortcut or its initial letters.
    • On the main menu, choose ReSharper | Edit | Insert Live Template.
    • Press Ctrl+J.
  3. Select a template from the suggestion list, and press Tab or Enter. The template expands into a code fragment.
  4. If the template is parameterized (i.e. requires user input), the editor switches to the template editing mode and suggests an input position for the first parameter. You can type an appropriate value, choose one of the suggested values, if any, or accept the default value suggested by ReSharper, and press:
    • Tab or Enter to move to the input position of the next parameter.
    • Shift+Tab to move the input focus to the previous input position.
    • Esc to switch off the template editing mode and move the caret to the end position defined by template developer. In this case, all parameters will be initialized with default values.

 Tip

Depending on settings of a particular template, redundant namespace references might be shortened automatically after template code is expanded. See Creating a Template for details.

Several examples that show how live templates are commonly applied are listed below.

Applying a template with a shortcut

  1. Type psvm (shortcut for "public static void Main") where you want to place the Main method:
  2. Press Tab to expand the template:

Selecting a template from the suggestion list

  1. Put the caret in a position where inserting an assertion method is appropriate.
  2. Press Ctrl+J:
  3. After selecting asrt in the suggestion list, press Enter or Tab:

Reducing the suggestion list

  1. Press Ctrl+J
  2. Then type it and select the desired template from the suggestion list:

Using a parameterized template

itar is the abbreviation for a template that contains code for iterating over an array:

When you press Tab (the template invocation key), the template expands replacing the "itar" abbreviation with template code, with suggested values for the template's "variables" already inserted, as shown in the following figure:

Keep in mind that the editor is now in its special template editing mode. You can see that by the presence of the red border around the index variable, which is the first input position in this template. Note that ReSharper automatically suggested i as the name for the index variable. If you want to change the suggested value, just type a new name. All references to the variable within the expanded template will be automatically changed as you type the new name. In the following figure, i has been changed to idx:

Regardless of whether you have changed the suggestion at the current input position, pressing Tab or Enter moves the caret to the next input position defined by the template:

ReSharper automatically detected all array variables in the scope, and suggested selecting one of them (namely, the last declared one). Had there been only one array element in the scope, ReSharper would have inserted its name automatically (without popping up a list). You can control pop-up suggestion lists using the following keys:

  • Use the Up and Down arrow keys to navigate through the list.
  • Use Esc to close the list and enter, for example, your own getArray() method call into this position.
  • Press Tab or Enter to apply the selected/specified option and move to the next input position.

ReSharper detects changes to the type of the array elements. It automatically updates the type of the variable holding the current array element, and suggests a suitable name for it.
After filling up all input positions, press Tab or Enter to get the caret moved to the specified end position of the template:

In many cases, default values suggested by ReSharper may be accepted, thus you can immediately proceed to writing the loop body after very few keystrokes.

 Tip

To get the most out of live templates, you can edit the existing live templates or create new live templates so that the set of your templates will best fit your development practices.

See Also

Concepts

Procedures

Reference

External Links