Intellij IDEA live templates
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
Tab and get a full for loop created for you, or type
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.
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
$END$means where to place the cursor after template expansion.
- 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.