“Live Templates” is one of many features that JetBrains software comes with. It is common that developers need to repeat certain lines of code over and over again. When you are fed up with writing the same piece of code every time you need it, then you start looking for it in the sources to simply copy and paste it. Not only is this very time-consuming, but also frustrating. It is easy to imagine that you need to repeatedly use Ctrl + C and Ctrl + V when in a hurry, and using a mouse to select a text makes it even harder. In such cases, live templates can save your time.
A template contains a predefined chunk of code that is inserted into a file. For each template, you can specify the sequence of keys that will insert the template into your code. You can configure a different set of templates for every type of file.
Defining a template
In order to add a new template, or redefine an existing one, you have to open File> Settings (or press Alt + Ctrl + S). The template configuration panel is available in the Editor> Live Templates submenu. You can also find it using the Search function.
The configuration window includes the following:
- By default expand with – a small but very important option. Here you can define the key you will use to confirm the template insertion. By default, it is the Tab key. The purpose of this key might be slightly unclear, but I’ll explain this later in this post.
- The list of template groups and already defined templates
- Typical + and – icons
When creating a new template (with the the + button) you have to choose whether you want to add a group or a template. Let’s select Live Template. The main window will be extended with additional inputs:
- Abbreviation – a literal shortcut that represents the template
- Template text – the area where you can define the template’s source code
- Description – the template’s description
- Options – a place where you can configure additional options, such as the expand key
When you have configured all of the above inputs, you have to define a range in which the template can be inserted. To do this, press the Define button (under the Template Text field).
One of the most powerful options for templates is the possibility to use variables. In order to insert a variable into the template text, you have to use the following notation: $<variable_name>$, e.g. $NAME$. When at least one variable is used, the Edit variables button is enabled. It allows you to open the Edit variables window which displays the table with all of the available variables (the first column), the action performed when inserting the template (the second column), and the default value (the third column). Additionally, you can configure if the cursor should be moved to the next variable if the current variable has a default value.
The second column is the most interesting. By defining the action that will be performed when inserting the variable, you can decide what the variable will evaluate to, e.g. an hour or a date.
* Expression: fileName()
* Expression: time()
* Expression: user()
* Expression: clipboard()
die(__CLASS__ . ", line: " . __LINE__);
All of the available actions are described in the official documentation.
Inserting the template into the source code
Inserting a template is a very quick action. Everything you have to do is to press the sequence of keys that you set as the template’s abbreviation, and then confirm with the confirmation key (remember that “By default expand with” option?). For example, if you set vd as an abbreviation, and the confirmation key is Tab, then you have to press v, d, and Tab in a sequence
Here you can see the difference between live template’s shortcuts and regular IDE shortcuts. The former are key sequences, the latter are key combinations that should be pressed simultaneously.
If any variable has been defined, the cursor will be moved to its location. Of course, if you selected Skip if defined, the cursor will skip corresponding variable, and the defined action will be performed, e.g. the date will be inserted.
There is also another way of inserting the template. You can just press Ctrl + J and select the template from the list.
The list of templates provided by your favourite IDE is worth reviewing, as it includes the most common and useful constructions. All of them are configured with very intuitive shortcuts.
The time you spend will pay off when you remember that these templates should help you be more productive. If you have too many of them or they are not intuitive enough, you probably just wasted your time configuring and learning them.
Edited by: Anna