Tuesday, 16 April 2013

newo shortcut (live template) in Intellij Idea

What I missed from Netbeans when I moved to Intellij Idea was the "newo" shortcut. Basically, "newo" + TAB will expand to

Object object = new Object();

allowing you to change the type and the variable name. Pretty useful when using long type names. I was not aware of anything similar in Intellij Idea, so I implemented it myself (this works in Intellij Idea 12.1.1):

First, go to Settings (CTRL+ALT+S or File -> Settings) and select "Live Templates" from the list on the left.

Add a new template (ALT + INSERT or hit the green "+" button on the right.

Type "newo" in the abbreviation box and whatever you want in the description. Then type

$TYPE$ $NAME$ = new $TYPE$();

In the template text. Those are live template variables that have to be defined and we'll do so by clicking Edit Variables. For $TYPES$ type typeOfVariable("java.lang.Object") in the Expression column and use "Object" as Default value. For $NAME$ just type suggestVariableName() in the Expression column, hit OK and you're done. :)

No comments:

Post a Comment