Create Reusable Live Templates in IntelliJ IDEA to Eliminate Boilerplate
Step-by-step guide to creating, testing, and sharing IntelliJ IDEA Live Templates with variables, context filtering, and export for team reuse.
14 Jun 2026, 04:47 UTC

Desired Outcome
Define a Live Template that expands a short abbreviation into a frequently used code block — such as a logger declaration, a JUnit test skeleton, or a try-with-resources pattern — with editable placeholders that you can tab through. The template should only appear in the appropriate language context (e.g., Java class files) and be portable across team members via settings export.
Prerequisites
- IntelliJ IDEA 2023.1 or later (Community or Ultimate). The Live Templates UI has been stable since 2020, but variable expressions like
className()behave consistently only in recent releases. - A project open with at least one Java file so you can verify context filtering.
- Write access to the IDE configuration directory (default location:
~/Library/Application Support/JetBrains/IntelliJIdea2023.xon macOS,%APPDATA%\JetBrains\IntelliJIdea2023.xon Windows,~/.config/JetBrains/IntelliJIdea2023.xon Linux).
Procedure: Create a Logger Template
1. Open the Live Templates Settings
Press Ctrl+Alt+S (Windows/Linux) or Cmd+, (macOS) to open Settings. Navigate to Editor → Live Templates. You’ll see a tree of template groups on the left (e.g., Java, JUnit, other).
2. Add a New Template to the Java Group
- Select the Java group (create it via the
+→ Template Group if missing). - Click
+→ Live Template. - Fill in:
- Abbreviation:
log(short, memorable, unique within the Java context). - Description:
SLF4J logger declaration. - Template text: paste the block below.
- Abbreviation:
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger($CLASS$.class);$END$3. Define the Context
Click the Define link next to “Applicable in” and check Java → Declaration. This restricts expansion to inside a class body, not inside methods or comments.
4. Configure the Variable
Click Edit variables. For $CLASS$ set:
- Expression:
className()— resolves to the current class name automatically. - Default value: leave empty (the expression supplies it).
- Skip if defined: checked — prevents the cursor from stopping on this variable since it’s auto-filled.
Leave $END$ as the final cursor position after expansion.
5. Save and Test
- Click OK to close the variable dialog, then Apply in Settings.
- Open a Java class file, type
logat the class level, and press Tab (default expansion key). The template expands to:
with the cursor after the semicolon (private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MyClass.class);$END$). - Verify that typing
loginside a method does not trigger the template (context enforcement).
Expected Checks
- Expansion works: Abbreviation + Tab produces the exact code block with correct class name.
- Context respected: No expansion in XML, properties, or inside method bodies.
- Variable behavior:
$CLASS$is replaced automatically; cursor lands at$END$without extra Tab stops. - No abbreviation collision: If you already have a
logtemplate in another group (e.g., “other”), the Java-group one wins inside Java declarations because context priority is group-order dependent. Reorder groups via drag-and-drop if needed.
Sharing the Template (Portability)
To distribute the template to teammates or reuse on another machine:
- File → Manage IDE Settings → Export Settings.
- Check only Live templates (uncheck everything else to avoid overwriting keymaps, inspections, etc.).
- Save the
settings.zip. - Recipients import via File → Manage IDE Settings → Import Settings and select the zip.
Alternatively, copy the template XML directly from <config>/templates/user.xml (created after first custom template) into version control and symlink or script its placement. The XML format is stable across 2023.x–2024.x.
Limitations & Gotchas
- Expression scope:
className()returns the innermost class name. In nested classes it yields the inner class name, which may not be what you want for a logger. UseclassName(true)for the outer class (undocumented, may change). - Duplicate abbreviations: If two templates in the same context share an abbreviation, the first in the list wins silently. Keep abbreviations unique per context.
- No multi-cursor expansion: Live Templates expand at the primary caret only. For repeated patterns across multiple lines, use Structural Search and Replace or a plugin like MultiTemplate.
- Kotlin context: The same template won’t appear in
.ktfiles unless you also check Kotlin → Declaration in the context dialog. Kotlin’s logger idiom often usesLoggerFactory.getLogger()without.class, so consider a separate Kotlin template.
Recovery / Rollback
If a template behaves unexpectedly:
- Return to Editor → Live Templates, select the template, and click the Revert button (appears after modification) to restore the built-in version if it was a modified default.
- Delete the custom template via the
-button. - To reset all Live Templates to factory defaults, delete
user.xmlin the templates directory and restart the IDE. - Create test template
test→System.out.println("$END$");in Java context. - Type
test+ Tab in a Java class → cursor inside quotes. - Type
test+ Tab in an XML file → no expansion. - Export settings, import into a clean IDE profile → template appears and expands.
Quick Verification Checklist
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.