Live templates
Expand a live template: Tab
Configure: CtrlAlt0S Settings | Editor | Live Templates
Use live templates to insert common constructs into your code, such as loops, conditions, declarations, or print statements.
To expand a code snippet, type the corresponding template abbreviation and press Tab. Keep pressing Tab to jump from one variable in the template to the next one. Press ShiftTab to move to the previous variable.
Before | After |
---|---|
The following demo illustrates the usage of live templates, along with the Extract function, Extract parameter, and Extract lambda parameter refactorings:
The following types of live templates are distinguished:
Simple templates contain only fixed plain text. When you expand a simple template, the text is automatically inserted into your source code, replacing the abbreviation.
Parameterized templates contain variables that enable user input. When you expand a parameterized template, variables are either replaced by input fields for the user to specify manually, or calculated by CLion automatically.
Surround templates wrap a block of the selected code with the text specified by the user. For example,
T
expands into a pair of tags, for which you can specify a name. You can also select a block of code, then press CtrlAlt0J to open the Select Template popup and select theT
template to wrap the selection with a pair of tags.
Postfix code completion is similar to live templates. It transforms the current expression without selecting it. For example, you can type .if
after an expression to invoke the corresponding postfix completion and wrap the expression with an if
statement.
To configure live templates, open the Editor | Live Templates settings page CtrlAlt0S. On the Live Templates page, you can see all the available live templates, edit them and create new templates.
Templates are grouped according to the context where they can be used (usually, by the corresponding language). To move a template to another group, right-click the template, select Move, and then select the necessary group name.
note
If you set a dialect for a live template, this live template will work only in this dialect.
Each live template is defined by an abbreviation containing alphanumeric characters, dots, and hyphens. The abbreviation must be unique within a group, but the same abbreviation may be used in different groups and expand to different constructs according to the context of the corresponding group . Abbreviations of modified default templates are shown in the list with blue font.
Only the templates enabled in this dialog are displayed in the editor. If a template is active, the editor is sensitive to its abbreviation. Otherwise, the abbreviation is considered a set of characters.
In the Template Text field, you can type the template body that 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 $$
.
tip
The Edit Variables button is enabled only if the template body contains at least one user-defined variable, that is, a variable different from
$END$
or$SELECTION$
.
For more information, refer to Create live templates.
Thanks for your feedback!