VOOZH about

URL: https://www.javacodegeeks.com/2014/01/code-faster-with-intellij-idea-live-templates.html

⇱ 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/>

👁 dep

  • pl – Inserts <plugin/>

👁 pl

  • repo – Inserts <repo/>

👁 repo

Iterations

  • fori – creates iteration loop

👁 fori

  • itar – iterates elements of array

👁 itar

  • itco – iterates elements of java.util.Collection

👁 itco

  • iter – iterates Iterable

👁 iter

  • itit – iterates java.util.Iterator

👁 itit

  • itli – iterates elements of java.util.List

👁 itli

Other

  • ifn – Inserts “if null” statement

👁 ifn

  • inn – Inserts “if not null” statement

👁 inn

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

👁 inst

  • lazy – Performs lazy initialization

👁 lazy

Plain

  • psf – public static final

👁 lazy

  • psfi – public static final int

👁 lazy

  • psfs – public static final String

👁 lazy

  • thr – throw new

👁 lazy

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 our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

👁 Photo of Maciej Walkowiak
Maciej Walkowiak
January 28th, 2014Last Updated: January 27th, 2014
6 304 1 minute read
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

6 Comments
Oldest
Newest Most Voted
mp
12 years ago

I very often use this template:

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

1
Reply
12 years ago

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 .

0
Reply
12 years ago

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.

0
Reply
11 years ago

Thanks for sharing , very useful :)

0
Reply
arun arora
11 years ago

all are available in eclipse :-)

0
Reply
11 years ago

That’s right. There are some other things available in eclipse. In former times there was “JBuilder”…

0
Reply
Back to top button
Close
wpDiscuz