π‘ IntelliJ IDEA β Code Snippets (Live Templates & File Templates)¶
π§ Introduction¶
IntelliJ IDEA supports two powerful snippet systems:
-
Live Templates β dynamic, context-aware snippets you trigger manually inside existing files (e.g., typing
yfmβ Tab). They use IntelliJ macros likedate(),className(), andclipboard()to inject live data. -
File Templates β static, prefilled structures used when creating new files. They use Velocity-style variables like
${NAME},${DATE}, and${YEAR}-${MONTH}-${DAY}.
Together, they automate everything from boilerplate code to note headers and logging patterns β turning IntelliJ into a serious productivity engine.
π§© PART 1 β Live Templates¶
βοΈ What Live Templates Are¶
Live Templates let you expand small abbreviations into complete code or text blocks. Theyβre ideal for repeating structures (YAML headers, annotations, logging, test stubs, etc.) and can include dynamic macros that automatically fill in data such as the date, file name, or user.
Path:
Settings β Editor β Live Templates
π§± Example β Markdown Front Matter with Auto Date¶
Abbreviation: yfm
Template text:
Variable setup (Edit Variablesβ¦):
| Variable | Expression | Stop at? | Description |
|---|---|---|---|
TITLE |
(empty) | β | You fill it manually |
DATE |
date("yyyy-MM-dd") |
β | Auto-fills todayβs date |
SUMMARY |
(empty) | β | Optional summary |
Now type yfm β press Tab β IntelliJ expands to:
β‘ Insert or Trigger a Live Template¶
| Action | Shortcut | Description |
|---|---|---|
| Expand template | Tab |
Type abbreviation and press Tab |
| Show available templates | Ctrl + J (Win/Linux) / βJ (macOS) |
Lists all templates valid in this context |
| Surround selected text | Ctrl + Alt + J / β₯βJ |
For templates with $SELECTION$ |
| Manage templates | Settings β Editor β Live Templates |
Template editor |
π§° Commonly Used IntelliJ Macros¶
| Macro | Example Output | Description |
|---|---|---|
date("yyyy-MM-dd") |
2025-10-17 |
Current date (custom format) |
time("HH:mm") |
08:42 |
Current time |
user() |
edgaras |
Your system/IDE username |
clipboard() |
(clipboard text) | Pastes clipboard content |
className() |
MainController |
Current class name |
methodName() |
getUserById |
Current method name |
packageName() |
com.example.app |
Current package |
fileName() |
UserService.java |
File name |
fileNameWithoutExtension() |
UserService |
File name stripped of extension |
uuid() |
2a4e... |
Generates a UUID |
selection() |
(selected code) | Used in Surround templates |
capitalize(β¦) |
Hello |
Capitalizes text |
snakeCase(β¦) |
my_variable |
Converts to snake_case |
camelCase(β¦) |
myVariable |
Converts to camelCase |
prompt("Label") |
(asks user) | Prompts for input |
π‘ Power Tips¶
- Prefix abbreviations by category (
md_,j_,r_, etc.) to keep lists organized. $END$marks where the cursor lands after expansion.$SELECTION$allows templates that wrap selected text.-
Combine macros:
-
capitalize(fileNameWithoutExtension())βMyFile camelCase(clipboard())β convert copied text to variable nameuuid().substring(0,8)β short random ID
ποΈ PART 2 β File Templates¶
βοΈ What File Templates Are¶
File Templates define prefilled content for new files. When you create a new file (e.g., βNew β MD Noteβ), IntelliJ uses these templates to populate default text.
They use a simpler syntax β Velocity variables β which look like ${VARIABLE}.
These are resolved at creation time, not live while editing.
Path:
Settings β Editor β File and Code Templates
π§± Example β Markdown Note Template¶
Name: MD Note
Extension: md
Template text:
Result when creating a new file:
π Common File Template Variables¶
| Variable | Example Output | Description |
|---|---|---|
${NAME} |
my-file |
New file name |
${USER} |
edgaras |
Current system user |
${DATE} |
17/10/2025 |
Localized date |
${TIME} |
09:12 |
Current time |
${YEAR} |
2025 |
Current year |
${MONTH} |
10 |
Current month |
${DAY} |
17 |
Current day |
${PACKAGE_NAME} |
com.example |
Java package |
${CLASS_NAME} |
UserService |
Derived from file name |
Velocity Tip: You can use expressions like:
to generate unique timestamps for file names.
π‘ Power Tips¶
- Use File Templates for your recurring file types: configuration files, test classes, documentation stubs.
- Use
${DATE}for locale-aware date or${YEAR}-${MONTH}-${DAY}for ISO-style. - Add includes for shared blocks (Settings β File and Code Templates β Includes tab).
- Works great combined with Live Templates β start from a File Template, enhance later with dynamic snippets.
π§ Quick Reference¶
| Template Type | Purpose | Syntax | Trigger |
|---|---|---|---|
| Live Template | Dynamic snippets inside existing files | $VARIABLE$ |
Abbrev + Tab |
| File Template | Prefilled structure for new files | ${VARIABLE} |
File β New |
| Postfix Template | Inline transformation (.if, .for, .nn) |
β | After expression + Tab |
| Surround Template | Wrap selected code | $SELECTION$ |
Ctrl+Alt+J / β₯βJ |
π§ Summary¶
Live Templates β dynamic, context-aware snippets for existing files.
Use IntelliJ macros such as date(), className(), clipboard(), uuid(), and user() to auto-fill information.
File Templates β static, prefilled structures for new files.
Use Velocity-style variables like ${NAME}, ${DATE}, ${YEAR}-${MONTH}-${DAY} to scaffold default content.
Both systems complement each other:
- Live Templates automate repetitive typing.
- File Templates provide consistent starting points. Together, theyβre the backbone of a fast, error-free workflow inside IntelliJ IDEA.