Mastering your IntelliJ IDEA: The power of Live Templates
Live templates can help us to master productivity. They are short abbreviations that we can use to write boilerplate code. They have a huge power and can do many things.
IntelliJ already provides us with some live templates and the power to create our own live templates. Let’s see how we can do that.
SOUT
When did you last need to do a System.out.println()? What you have done, do you write all of this manually?
I hope not, but if you are doing it manually, I have good news.
You can write “sout” and then press enter, it will generate the System.out.println();
For Loop
What was the last time you wrote a for iteration?
In Java, there are many ways to do that, you can write a for with an increment index, a for iterable, or a forEach. All of them can be used with a live template.
You only need to take your LIST, and press .for
Throw new
How about throwing a new exception? You can, of course, write it, in this case is not a big thing, but we also have a live template for this code.
You can use the live template thr, and it will generate the code throw new code.
Create your own live templates
Now it’s your time, you can create your live templates, and it is very easy.
Select a code that you usually need to create manually, press Shift + Shift and write “Save as Live Template”, this will open a window where you can configure your live template.
You can choose the name you want, and also define variables. Let’s see one example:
Now if we write “list” we can use our live template to generate an ArrayList.
This is only a simple example, but we can do many more things, now it’s your turn, to create and share your own live templates 😁
Conclusion
Live templates are a useful tool that IntelliJ provides to us. We can also define our own live templates, which can empower our productivity and our daily activities.
If you know other live templates or enjoy this tip, please share in the comments 😁