Live templates
A live template is an abbreviation that expands into a code snippet with fields to fill in. Type the abbreviation and press⇥ / Tab, or pick it from the completion list. For example, in Java sout becomes System.out.println(); with the caret inside the parentheses; in Rust fn expands into a function skeleton.
After expansion, the caret is in the first field.
⇥ / Tab moves to the next field and fields that share a name change together. Esc leaves the template.
Built-in templates exist for Rust, JavaScript / TypeScript, Python, Go, Java, Kotlin, PHP and SQL, plus a few general ones (todo, fixme, hack, date). Insert Live Template… lists the templates that apply at the caret. By default templates expand only in code, not in comments or strings.
Surround With
Select code and press⌥⌘T / Ctrl+Alt+T. The list starts with the language’s wrappers (if, while, try and so on), followed by live templates that use $SELECTION$. Surround with Live Template… shows only the latter.
Postfix completion
Postfix templates rewrite the expression before a dot. Type an expression, a dot and the template key, then press⇥ / Tab:
if, else, not, var, return, for, fori, nn (not null), null, par (wrap in parentheses) and try; each language adds its own, such as match, ok and some in Rust, sout in Java and Kotlin, when in Kotlin, await in JavaScript / TypeScript and Python.
Enable or disable postfix completion, individual templates, Tab expansion and whether postfix templates appear in the completion list in Settings › Editor › General › Postfix Completion.
Emmet
In HTML, Vue and Svelte files, and in CSS, SCSS, Less and Sass,⇥ / Tab expands Emmet abbreviations, for example ul>li*3 or m10. In markup files, the editor context menu also has Update Tag with Emmet, which rewrites the tag at the caret from an abbreviation.
Write your own templates
Templates are edited in Settings › Editor › Live Templates.1
Add a template
Click Add Template and enter the Abbreviation, Description and Group.
2
Write the body
Use
$NAME$ for a field (repeated names are linked), $END$ for the final caret position, $SELECTION$ for the
text being surrounded and $$ for a literal dollar sign. Leading tabs in the body are indent units.3
Choose contexts
Pick the languages where the template applies. Check Also expand in comments and strings if it should expand
there too.
4
Give fields values (optional)
In the variables table, set an Expression, a Default value, and Skip if defined to jump over a field
whose expression produced a value.
className(), methodName(), fileName(), fileNameWithoutExtension(), date("yyyy-MM-dd"), time(), user(), clipboard(), complete(), suggestVariableName(), commentStart(), capitalize(…), camelCase(…) and snakeCase(…).
For example, this template logs a variable:
Macros
A macro records the actions you run, the text you type and the editing keys you press in the editor, so you can replay them. The commands are in Edit › Macros:1
Record
Choose Start Macro Recording. The status bar shows that a macro is recording.
2
Stop
Choose Stop Macro Recording and enter a name, or leave the name empty to keep it only as the last macro.
3
Play back
Play Back Last Macro replays the last recording. Play Saved Macros… picks a saved one.