Intellij IDEA live templates

Intellij IDEA IDE has a nice feature of live templates. You type several symbols, press tab (by default) and it expands the symbols into full expressions. E.g. you can type fori, press Tab and get a full for loop created for you, or type iter, press Tab and if you have iterables in scope, you’ll be presented with a choice of available ones to iterate over.

Example: slf4j logger in each class

It’s a super common thing to type at the beginning of each class:

private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(MyClassName.class);

pretty tedious even with IDE’s help.

Live templates to the rescue, you can define your own one to create the whole line for you! You

How to define a live template

  • Go to File->Settings->Editor->Live Templates.
  • In the right panel tree select category other.
  • Click the plus (+) sign on the top right, select Live Template.
  • Set
    • Abbreviation: log
    • Description: Inserts private static Logger for slf4j
    • Template text: private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger($CLASS_NAME$.class);$END$
  • Now click the Edit Variables button, we will tell the IDE what $CLASS_NAME$ means here. $END$ means where to place the cursor after template expansion.
    • Name: CLASS_NAME
    • Expression: className()
    • Default value: leave empty
    • Skip if defined: true (check the checkbox)
  • At the very bottom look for text Applicable in with a link Change next to it, click it. Select Java->declaration.

Congratulations, you’re done! Just type log and press Tab anywhere in the class declaration.

Principal Bioinformatics Scientist

My main research interests are data visualization and signal processing for mass spectrometry.