Creating and Editing Live Templates
RubyMine comes with a set of the predefined Live Templates. You can use them as is, or modify them to suit your needs. If you want to create a new live template, you can do it from scratch, on the base of the copy of an existing template, or from a fragment of source code.
If a template has been changed, it is always possible to restore its default settings.
Modifying existing templates
To modify an existing template
- In the Settings/Preferences dialog, open the Live Templates page.
- Expand the desired template group, and select the template you want to change.
- In the Template Text area, change the template abbreviation as required.
- In the Template Text field, edit the template body which may contain plain text and variables in the format
$<variable name>$
. If necessary, click Edit Variables and configure the variables as required. To escape a dollar sign ($
) in the template text, just duplicate it ($$
). - In the Options section, specify how the template will be expanded and reformatted.
- In the Available in section, specify the languages and places of code where the editor should be sensitive to the template abbreviation.
- Click OK when ready.
Side note about predefined template variables
RubyMine supports two predefined live template variables $END$
and $SELECTION$
, both of them are read-only.
$END$
indicates the position of the cursor after the template is expanded. For example, the templatereturn $END$;
will be expanded intoreturn ;
with the cursor positioned right before the semicolon.$SELECTION$
is used in surround templates and stands for the code fragment to be wrapped. After the template is expanded, the selected text is wrapped as specified in the template. For example, if you selectEXAMPLE
in your code and invoke the"$SELECTION$"
template via the assigned abbreviation or by pressing Ctrl+Alt+T and selecting the desired template from the list, RubyMine will wrap the selection in double quotes as follows:"EXAMPLE"
.
Creating a new live template from scratch
To create a new template from scratch
- In the Settings dialog, open the Live Templates page, and expand the template group where you want to create a new template.
- Click . A new template item is added to the group and the focus moves to the Template Text area.
- Specify the new template abbreviation, type the template body, define the variables and the template group, configure the options, as described in the template modification procedure.
- Click OK when ready.
Creating a new live template from a text fragment
To create a live template from a text fragment
- In the editor, select the text fragment to create a live template from.
- On the main menu, choose Live Templates page opens, with the Template Text area in focus. . The
- In the Abbreviation field, type abbreviation to identify your new live template.
- Specify the new template abbreviation, type the template body, define the variables and the template group, configure the options, as described in the template modification procedure.
- Click OK when ready.
Searching through the list of live templates
To search through the list of live templates
- In the Live Templates page, start typing any string, which you expect to exist in the template abbreviation, body, or description.
RubyMine shows all matching templates:
Restoring defaults
To restore default settings of a template
- In the Live Templates page, right-click a modified template to reveal its context menu.
- Choose Restore defaults on the context menu of the modified template.
Last modified: 4 April 2018