Live templates in IntelliJ IDEs

Evagoras Frangou
5 min readNov 4, 2024

--

As developers, we often find ourselves repeatedly writing similar code patterns when creating classes such as ViewModels, databases, or — if you’re a Flutter developer — state management classes like Bloc, Event, and State. Copying and pasting code can lead to bugs and incorrect naming, especially if names aren’t updated consistently across files.

This is where Live Templates come in. Live templates act as shortcuts for frequently used code blocks, helping streamline the coding process. IntelliJ IDEs come with several built-in templates, such as:

  • Android Kotlin: fun1 (used for generating a function with parameters)
  • Flutter: stful (generates a Stateful widget template)

Now, let’s see how to create custom live templates to meet specific project needs.

Step 1: Navigate to Live Templates Settings

  1. Open the Settings in your IntelliJ IDE.
  2. Use the search bar to type Live Templates or find it manually under the Editor settings.
1.0 Navigate to Live Templates Settings

Step 2: Create a new Template

You can create a new template in one of two ways:

  1. Add to an Existing Group:
  • First, select the template group where you’d like your template to belong.
  • Click the + icon and select Live Template.

2. Create a New Group:

  • Click the + icon and choose Template Group.
  • Name the group, then add templates as needed by following step 1.
2.0 Create a new Template

Step 3: Unterstand Live Template Settings

To create a custom live template, you’ll need to understand the following fields:

  1. Abbreeviation: The shortcut you’ll type to insert the template (e.g. fun1).
  2. Description: A brief text describing what the template does.
  3. Template text (script): The code that will be generated when you type the abbreviation.
  4. Edit variables: Allows you to define placeholders or dynamic fields that accept values when the template is used.
  5. Define application context: Specifies the programming language or context (e.g., Kotlin, Dart, Java) in which the template can be used.
3.0 Unterstand Live Template Settings

Step 4: Write Your Template

Let’s look at an example of fun1. This template uses variables like $PARAM1$ and $END$. Note that:

  • Variables must be surrounded by $ symbols (e.g. $PARAM1$) to be recognized by the template.
  • $END$ indicates the end of the template code.
4.0 Write Your Template

Step 5: Setup Template Variables

It’s important to know that if the template code does not include any variable the Edit Variable button will not be enable. Looking at the fun1 we have 4 variable. Let’s check from what they are consist.

  1. Name: Defines the variable’s identifier (e.g., PARAM1, RETURN).
  2. Expression: Optionally manipulate the variable’s value using predefined functions (for a list of expressions, see IntelliJ’s Template Variables documentation). To reference any other variable within an expression, simply use the variable name without the $ symbol.
  3. Default Value: Sets an initial value for the variable.
  4. Skip if defined: If checked, the IDE won’t prompt for the variable value if it already exists.
5.0 Setup Template Variables

Step 6: Define where template will be used

Finally, define the scope of your template. For instance, checking the screenshot fun1 is configured to be used within Koltin classes, statements and as top-level function.

6.0 Define where template will be used

Congratulations on making it this far! As a thank you, here’s an additional example of how to set up a variable with an expression. In this example, we’ll look at a Flutter live template using the stfu shortcut for creating a StatefulWidget class, and we’ll explore how expressions work with the template variables.

In the example below, there are two key variables:
1. Name: This variable simply captures the widget name.
2. SNAME: This variable utilizes two expressions to generate its value:

  • concat: According to the official documentation, the concat function is structured ad (<String>, …). This means it takes multiple strings and combines them. In our example, it adds an underscore (_), followed by the value of NAME and finally appends the word State.
  • regularExpression: As per the official documentation, the structure of regularExpression is regularExpression(<String>, <Pattern>, <Replacement>). This function takes the given string, searches for a specified pattern, and replaces it with the provided replacement. In this case, it takes the result of the concat expression, checks if it starts with two underscores, and replaces any instance of this with a single underscore.

Based on the explanation above, if the NAME of your StatefulWidget is VechicleWidget then the SNAME will be _VechicleWidgetState.

Demostration of stful Live Tempalte

Thank you for reading! I hope this guide helps you create effective custom live templates. If you have any questions, suggestions or would like an example of creating a fully custom template, please feel free to share them in the comments.

References

IntelliJ Live Templates Documentation

--

--

Evagoras Frangou
Evagoras Frangou

Written by Evagoras Frangou

BSc Computer Science, Senior Software Engineer/Team Lead, Android and Flutter Developer Find out more example projects on my GitHub: https://github.com/r1n1os

Responses (1)