IntelliJ IDEA allows you to save some keyboard strokes by quickly inserting code that you use frequently. This feature is called live templates (similar to user snippets in Visual Studio Code).
You can find them in the Settings:
I have created a new group named “Custom” and inside that group I have three live templates. As an example, this is a basic template that checks if an argument is null:
if ($name$ == null) {
throw new IllegalArgumentException("$name$");
}
The $name$
expression is a variable placeholder.
The abbreviation of this live template is argnull
so if I start typing
argnull
in the IDE it appears as a suggestion:
and by hitting the Tab key, the template expands:
The live templates are stored as XML in the IntelliJ configuration directory, in
my case that is ~/.IdeaIC2019.3/config/templates
.
Another example is defining a logger for a class:
private static final Logger LOGGER = LoggerFactory.getLogger($CLASS_NAME$.class);
We can provide an expression for the CLASS_NAME
variable so that it will be
automatically replaced by the class name of the class where the live template is
expanded into. In our case it’s the className()
function:
The documentation lists the available predefined functions.