Top 10 Interesting NetBeans IDE Java Shortcuts I Never Knew Existed
I'm working on updating the NetBeans IDE Keyboard Shortcut Card (which you can always find under the "Help" menu in the IDE) and have learned about a lot of shortcuts I never knew about before. Here's a grab bag of things I have added to the shortcut card (some new, some old that hadn't been included before) that you might find interesting too.
- Type "fcom" (without the quotes, same as all the rest below) and then press Tab. You now have this, i.e., a brand new code fold:
// <editor-fold defaultstate="collapsed" desc="comment">
// </editor-fold> - Type "bcom" and then press Tab to create the start of a new set of comments in your code:
/**/
- Type "runn" and press Tab and you'll have all of this:
Runnable runnable = new Runnable() {
public void run() {
}
}; - If I have this:
String something = "";
...and then below that type "forst" and press Tab, I now have this:
String something = "";
for (StringTokenizer stringTokenizer = new StringTokenizer(something); stringTokenizer.hasMoreTokens();) {
String token = stringTokenizer.nextToken();
}Also, experiment with "forc", "fore", "fori", "forl", and "forv"!
- I always knew that "sout" turns into "System.out.println("");" but did you know that (again assuming you first have a string something like above) if you type "soutv" you end up with this:
System.out.println("something = " + something);Thanks Tom Wheeler for this tip.
- Next, here are the new shortcuts that will work only in 6.9:
- as - assert=true;
- su - super
- db - double
- sh - short
- na - native
- tr - transient
- vo - volatile
- I knew that "ifelse" would resolve to an if/else block. But did you know that if you don't need an 'else', you can simply type "iff", press Tab, and then end up with this:
if (exp) {
} - In 6.9, the "sw" shortcut expands to the following:
switch (var) {
case val:
break;
default:
throw new AssertionError();
} - If you're using while loops, experiment with "whileit", "whilen", and "whilexp".
- Always remember these: "im" expands to "implements; "ex" to "extends".
Other tips along these lines are more than welcome here on NetBeans Zone!





Comments
Peter McNeil replied on Fri, 2010/02/19 - 5:54am
Geertjan Wielenga replied on Fri, 2010/02/19 - 7:51am
David Stevens replied on Fri, 2010/02/19 - 12:43pm
Tom Wheeler replied on Fri, 2010/02/19 - 1:34pm
Great article, Geertjan. It's amazing how much time you can save over the course a week when you take the time to use a few timesavers in the IDE.
Of course, the NetBeans team can't foresee everyone's needs, so it's helpful to know how to add your own code templates. Just click Tools -> Options -> Editor -> Code Templates and define some new ones. I'd like to share a few I always define to make life easier:
The 'nyi' and 'ft' help me to ensure that I can create a new method or test case that I won't later overlook if I get interrupted before I'm able to complete it.
Milos Silhanek replied on Fri, 2010/02/19 - 5:51pm
Toni Epple replied on Mon, 2010/02/22 - 5:38am
in response to:
Geertjan Wielenga
I was wondering about fcom and bcom as well, but actually bcom and fcom have a secret :-)! If you look at the definition of the bcom template you'll see it's defined as:
/*${selection}*/
The interesting part is ${selection}. If you use this in a template, the system will know this is a "Surround with" quickfix. It means whenever you select some text in the editor a lightbulb will appear and offer you to surround your selection with your text, in this case it will surround it with /* */ and comment it out.
Try it out! In the Editor select some text. A lightbulb will appear with quick fix "Surround with /* */".
fcom works the same way. It only makes real sense if you select something and then make it foldable by clicking the lightbulb.
Daniel Sheppard replied on Tue, 2010/02/23 - 9:41am
Some interesting shortcuts here.
Personally I also always add the following as tst for JUnit tests
public void ${name}() throws Exception {${cursor}
Ben Hills replied on Wed, 2010/02/24 - 2:44am
Piotr Stolarczyk replied on Wed, 2010/02/24 - 8:29am
this, and all shortcuts are nice, but it would be great if we got a list of hints when typing "for" and hitting Tab so we can see a list of available shortcuts ("forc", "fore", "fori", "forl", "forv") just like whit code hints.
Brondon Lee replied on Wed, 2010/02/24 - 9:11am
This is why Netbeans is my favoriteIDE
Kenneth George replied on Wed, 2010/02/24 - 10:43am
Jirka X1 replied on Wed, 2010/02/24 - 12:38pm
Would be nice if these shortcuts were available in a similar way as code completion. In some old version of JBuilder I could press Ctrl+J and a list of code abbreviations appeared. In Netbeans, I often know there is a code abbreviation for the thing I am going to write, but it is too much hassle to find what it actually is, so I just type the full code.
Also for some reason typing "sout" + tab expands, but typing "sout" + space + delete + tab does not.
Sumit Navgire replied on Sat, 2010/02/27 - 2:17pm
Stop writing constructors, getter/setters. Anywhere in a class press Alt+Insert, select the option & netbeans will generate the code for you.
Select the lines that you want to place inside a for, while, do-while, if, try or a method and press Alt+Enter, select the option & netbeans will write the block around it.
Peter McNeil replied on Sun, 2010/02/28 - 6:15am
in response to:
Toni Epple
Daniel Nick replied on Tue, 2010/03/02 - 2:23am
Hussain Ali replied on Mon, 2010/03/08 - 5:25pm
Mike Morris replied on Wed, 2010/03/10 - 2:29am
"ex" -> "extends" has always particularly irritated me because it clashes with "catch( Exception ex )..." templates, so, in the following catch-block you frequently find yourself trying to type things like "throw new FooBarException( ex )" only to end up with "throw new FooBarException( extends )"... aaarggh!!leftarrow leftarrow leftarrow backspace backspace backspace backspace backspace
Irritating to the point where I deleted the "ex" editor template. The question is, "Which do you deal with more: defining classes that inherit, or catching exceptions?" I guess each person will have their own preference on this.
Ideally I would prefer to change the catch-clause templating, but could not find where to do that (and could not afford the time to investigate deeply enough.) So how do we change it?
Willis Morse replied on Mon, 2010/03/22 - 9:25am
Geertjan Wielenga replied on Sat, 2010/03/27 - 4:30pm
Ryan Fitz replied on Fri, 2012/03/02 - 2:27am
in response to:
Geertjan Wielenga
it helped me Geertjan!thanks for the tip
Matt Coleman replied on Fri, 2012/03/02 - 2:32am
i got that same problem too with the ex too
graphic artist buffalo