Boost Your Java Productivity with IntelliJ Live Templates: A Complete Guide
This article explains what IntelliJ Live Templates are, how they work, and provides a step‑by‑step guide with common built‑in templates, custom template creation, surrounding templates, and variable usage to dramatically speed up repetitive Java coding tasks.
Live Templates are a feature in IntelliJ IDEA that lets you insert predefined code snippets by typing short abbreviations, optionally with context‑aware variables, to reduce repetitive typing and improve development efficiency.
For example, typing
psfsand pressing Enter expands to
public static final String, saving you from manually typing the full declaration.
Common Live Templates
main and psvm
Insert a main method quickly.
<code>public static void main(String[] args) {
}
</code>Variable Declarations
psfs
<code>public static final String
</code>psfi
<code>public static final int
</code>prsf
<code>private static final
</code>St
<code>String
</code>Console Output
sout
Print a line to the console.
<code>System.out.println();
</code>souf
Print formatted text.
<code>System.out.printf();
</code>Loop Iteration
fori
Creates a for‑loop with the cursor positioned at the condition.
<code>for (int i = 0; i < ; i++) {
}
</code>iter
Iterates over a collection with a for‑each loop.
<code>List<String> array = new ArrayList<>();
for (String s1 : array) {
}
</code>itco
Iterates using an iterator.
<code>List<String> array = new ArrayList<>();
for (Iterator<String> iterator = array.iterator(); iterator.hasNext(); ) {
String next = iterator.next();
}
</code>Surround Templates
After selecting code, press
option+
command+
j(Mac) or use the
Code → Surround Withmenu (Windows) to wrap the selection with a template such as a
Callableimplementation or read/write lock handling.
Custom Templates
You can create your own templates for frequently used code, such as a StopWatch timing snippet, by opening
Settings → Editor → Live Templates → Java, clicking the plus sign, and defining the abbreviation, description, and template text with placeholders like
$MESSAGE$and
$SELECTION$.
<code>StopWatch stopWatch = new StopWatch("$MESSAGE$");
stopWatch.start();
$SELECTION$
stopWatch.stop();
System.out.printf("Execution time %s%n", stopWatch.toString());
</code>When the template is expanded, the cursor lands on
$MESSAGE$for you to customize the label, and the selected code is inserted at
$SELECTION$.
Supported Template Variables
Live Templates can include context variables such as
$CLASS_NAME$and
$METHOD_NAME$to automatically insert the current class or method name. For example, the built‑in
soutmtemplate expands to:
<code>System.out.println("$CLASS_NAME$.$METHOD_NAME$");
</code>Refer to the official IntelliJ documentation for a full list of predefined variables and functions.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.