Geertjan is a DZone Zone Leader and has posted 466 posts at DZone. You can read more from them at their website. View Full User Profile

Top 10 Interesting NetBeans IDE Java Shortcuts I Never Knew Existed

02.19.2010
| 46446 views |
  • submit to reddit

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.

  1. 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>
  2. Type "bcom" and then press Tab to create the start of a new set of comments in your code:
    /**/
  3. Type "runn" and press Tab and you'll have all of this:
    Runnable runnable = new Runnable() {

    public void run() {

    }
    };
  4. 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"!

  5. 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.

  6. 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

  7. 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) {

    }
  8. In 6.9, the "sw" shortcut expands to the following:
    switch (var) {
    case val:

    break;
    default:
    throw new AssertionError();
    }
  9. If you're using while loops, experiment with "whileit", "whilen", and "whilexp".

  10. Always remember these: "im" expands to "implements; "ex" to "extends".

Other tips along these lines are more than welcome here on NetBeans Zone!
Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Peter McNeil replied on Fri, 2010/02/19 - 5:54am

bcom[Tab] == /**/ and the saving is?

Geertjan Wielenga replied on Fri, 2010/02/19 - 7:51am

The saving is that you don't need to look for the asterisk and the slash, nor hold down the Shift key.

David Stevens replied on Fri, 2010/02/19 - 12:43pm

Thanks Geertjan! Some of those were new to me too. I like fast import: ALT-Shift-I

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:

 iaethrow new IllegalArgumentException("${cursor}"); 
 nyithrow new UnsupportedOperationException("TODO -- not yet implemented");
 ftfail("Not yet implemented");
 logdlogger.debug("${cursor}");
 logelogger.error("${cursor}");

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

Great! I must sometimes explore code templates. I have just explored settings and I setted mandatory brackets to if, while, for statement. Bye

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

@Test
public void ${name}() throws Exception {
    ${cursor}
}

Ben Hills replied on Wed, 2010/02/24 - 2:44am

Great article, most of them I didn't know so I've learnt something new today!

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

These are fantastic! BUT, it is important to note that not ALL shortcuts are available while editing. Try the same shortcuts while editing a non-.java file and you may be suprised. It would be even better if these WERE available for editing non-java files too - maybe in the next release after 6.9???

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

OK yeah that does make sense then.

Daniel Nick replied on Tue, 2010/03/02 - 2:23am

It was very nice experience to know all these ....

Hussain Ali replied on Mon, 2010/03/08 - 5:25pm

Short cuts are good . I am trying to use moors code for java code.which will give new life to java.

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

These are great, thanks. Makes the transition from Idea a little less painful :-) Is there an equivalent for Idea's "Expand selection" (cntl/command-W). It iteratively expands the selection each time you hit it, starting with words, then expressions, then blocks, etc. That's the one I miss more than anything else.

Geertjan Wielenga replied on Sat, 2010/03/27 - 4:30pm

Expand selection is Alt-Shift-period, the opposite way is Alt-Shift-comma. Does that help you, Willis?

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

Cata Nic replied on Mon, 2013/09/02 - 4:46am

 The ex too makes problems. I cannot have a stable solution because of this instability.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.