Code faster with Intellij IDEA live templates

Intellij IDEA – in my opinion the most productive IDE for Java – comes with bunch of features helping getting rid of writing repetitive code – which is a everyday business no matter what language you use. One of them is live templates.

Live templates contain predefined code fragments. You can use them to insert frequently-used or custom code constructs into your source code file quickly, efficiently, and accurately.

IDEA comes with a set of live templates for Java, Maven, HTML, CSS and more and also enables to create custom templates.
 

Inserting live template

There are 2 options to insert live template:

  1. Type live template abbreviation (for example dep) and next press TAB key
  2. Use shortcut Command + J (Mac) / Ctrl + J (PC) to popup list of available live templates in current context

At the beginning it’s quite difficult to remember all you wish to use and going back and forth from code to settings or browsing list is not very efficient – especially when you don’t know what you are looking for. That’s why I created gallery of (in my opinion) most useful built-in live templates for Java and Maven that can be used as a cheat sheet:

Maven

  • dep – Inserts <dependency/>

  • pl – Inserts <plugin/>

  • repo – Inserts <repo/>

Iterations

  • fori – creates iteration loop

  • itar – iterates elements of array

  • itco – iterates elements of java.util.Collection

  • iter – iterates Iterable

  • itit – iterates java.util.Iterator

  • itli – iterates elements of java.util.List

Other

  • ifn – Inserts “if null” statement

  • inn – Inserts “if not null” statement

  • inst – Checks object type with instanceof and down-casts it

  • lazy – Performs lazy initialization

Plain

  • psf – public static final

  • psfi – public static final int

  • psfs – public static final String

  • thr – throw new

Summary

Using predefined Java templates is just a beginning. You can find ready to use templates for particular frameworks on Github and I really encourage you to create your custom, project specific ones.

If you created interesting common use live templates for Java or related frameworks feel invited to post them in comments.

 

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

4 Responses to "Code faster with Intellij IDEA live templates"

  1. mp says:

    I very often use this template:

    private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger($CLASS_NAME$.class);

  2. wasim says:

    Hey Maciej Walkowiak ,
    that’s look cool and easy to code with it.
    can you provide me git hub link for this script. i want to use it in android studio .

  3. Hey Wasim,

    what script exactly you mean? Live templates are built in feature to Intellij IDEA – you don’t need any script to use it.

  4. Alican says:

    Thanks for sharing , very useful :)

Leave a Reply


four × 6 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close