Stop Typing Boilerplate: JetBrains Live Templates That Actually Save Time
JetBrains Live Templates turn repetitive boilerplate into single-abbreviation expansions with variables, scoping, and team sharing. Learn to create a Java main-method template that auto-fills the class name and positions the caret, plus how to export templates for team consistency.
10 Nov 2025, 18:00 UTC

The Problem: Boilerplate That Won't Write Itself
Every Java developer has typed public static void main(String[] args) { } hundreds of times. The same goes for loggers, builders, test scaffolding, and try-with-resource blocks. IDE autocomplete helps, but it still requires keystrokes and decisions. JetBrains Live Templates turn those repetitive patterns into a single abbreviation that expands into a full, editable code block — with the caret landing exactly where you need to start typing.
How Live Templates Work
A Live Template is an abbreviation (like main) bound to a template text that can contain variables written as $VAR_NAME$. Variables can have default expressions, skip-if-true conditions, and even Velocity-based logic for conditional sections. When you type the abbreviation and press Tab (or your configured expansion key), the IDE replaces the abbreviation with the template text, then enters a template editing mode where each variable becomes a field you can tab through. The special variable $END$ marks where the caret should land after you finish filling variables.
Variables are defined in the template editor with an expression (e.g., groovyScript("return 'log' + _1.capitalize()", className())) and an optional default value. If you check "Skip if defined", the variable won't stop the caret when its expression already resolves to a value.
Scoping: Templates Only Where They Belong
One of the most useful features is scope. Each template lives in a group (e.g., Java, Kotlin, XML, SQL) and can be further restricted to contexts like "class body", "method body", or "comment". This prevents accidental expansions — your log template won't trigger inside a string literal or a Gradle file. To set scope, open Settings → Editor → Live Templates, select the template, click Define next to "Applicable in", and choose the language and context checkboxes.
Worked Example: A Smarter Java Main Method
Let's create a template that expands main into a main method with a logger already declared and the caret positioned inside the method body.
- Open Settings → Editor → Live Templates (press Ctrl+Alt+S on Windows/Linux, ⌘+, on macOS).
- Select the Java group (create one with + if missing).
- Click + → Live Template.
- Abbreviation:
main| Description:Main method with logger. - Template text:
public static void main(String[] args) { private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger($CLASS$.class); $END$ } - Click Edit variables. For
$CLASS$, set Expression toclassName()and check Skip if defined. Leave$END$as-is (no expression needed). - Click Define → choose Java → check Class body (so it only works inside a class).
- Apply and close. In a Java class, type
mainand press Tab. The template expands,$CLASS$resolves to the current class name automatically, and the caret jumps to the line after the logger declaration.
Verification: The logger line should compile without red squiggles, and pressing Tab again should exit template mode, leaving you in normal editing.
Sharing Templates Across a Team
Templates are stored in templates/ under the IDE configuration directory. The easiest way to share them is File → Manage IDE Settings → Export Settings, check Live Templates, and save a .jar. Teammates import it via Import Settings. For continuous sync, enable Settings Repository (same dialog) pointing to a Git repo — changes push/pull automatically. Note: templates are per-IDE-version; a major upgrade (e.g., 2024.1 → 2025.1) may require re-import or minor adjustments if the schema changes.
Trade-offs and Limitations
- Complexity cost: Templates with many variables, nested Velocity logic, or heavy Groovy expressions can slow expansion noticeably. Keep logic simple; if you need a wizard, consider a plugin instead.
- Migration friction: Major IDE upgrades sometimes change the template storage format. Export before upgrading, then import and test.
- Scope creep: Over-scoping (e.g., making a template available in "everywhere") leads to accidental triggers. Start narrow (class body, method body) and widen only when needed.
Try It Today
Pick one boilerplate pattern you typed this week — maybe a JUnit test skeleton, a REST controller method, or a database migration snippet. Spend five minutes turning it into a Live Template with a single variable and a scoped context. Verify it expands cleanly, then export the settings and hand the .jar to a colleague. The time you save on the next occurrence pays for the setup immediately.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.