Quickstart NetBeans Editor Shortcuts and Code Templates

NetBeans is an optimal Java development environment. Just recently, while visiting the JAX Conference in Mainz/Germany, I attended a live coding session by Adam Bien. I was telling later a fellow developer and Eclipse user how convenient NetBeans is and how Adam, even though he broke his arm in a bicycle accident, used NetBeans during his live coding session. Later I met my colleague again during lunch time: he just had installed NetBeans on his notebook and was already fond of the NetBeans usage experience.

And yes, numerous keyboard shortcuts and code templates make editing and writing java classes with NetBeans a breeze. Here are my favourites to give you a quick start within the NetBeans Code Editor (in brackets the Eclipse pendants) :

Keyboard Shortcuts

  • Alt+ENTER (Ctrl+1)
    Fixes Code – whenever there is a yellow light bulb on the left hand side of the editor, try Alt+ENTER: a list of suggestions how NetBeans can fix the code for you pops up. It also may add missing JavaDoc and assigns a variable to the returned value of a method call.
  • Alt+Insert
    The code generation menu opens up and let’s you insert getters and setter, equals and hashCode, toString and many more.
  • Shift+ENTER (same)
    Insert a new line and move the cursor to the new lines beginning.  Shift+ENTER may be pressed at any position on the current line without splitting it.
  • Ctrl+e (Ctrl+d)
    Removes the current line.
  • Shift+Alt+period/comma
    Selects next/previous element (such as name, expression, line, method body, method etc).
  • Ctrl-r (Alt+Shift+r)
    Rename
  • Ctrl-o (Ctrl+Shift+t)
    Goto Type
  • Shift+Alt+o (Ctrl+Shift+r)
    Goto File
  • Shift+Ctrl+Arrow Up/Down (Ctrl+Alt+Arrow Up/Down)
    Copy line up/down
  • Shift+Alt+Arrow Up/Down (Alt+Arrow Up/Down)
    Move line up/down
  • Ctrl+Arrow Up/Down (same)
    Scroll without loosing cursor position
  • Shift+Ctrl+c (same)
    Comment/Uncomment line
  • Shift+Alt+f (Shift+Ctrl+f)
    Format file
  • Shift+Ctrl+i (Shift+Ctrl+o)
    Organize imports
  • Shift+Ctrl+v
    Insert clipboard content formatted
  • Ctrl+F12 (Ctrl+o)
    Select Navigator Window. Inside start typing method names and the matching method will be selected. Press ENTER to navigate to it in your source file. Very handy!
  • F7/F8 (F5/F6)
    Debugger: Step into/Step over
  • Ctrl+Shift+ENTER (Ctrl+m)
    Show only Editor (hit key combination a second time to return to normal)
  • Ctrl+s (same)
    Save File
  • Ctrl+g (Ctrl+l)
    Goto Line

Code Templates

Code Templates may be entered at any line in the current file. After hiting the TAB-Key, the template is expanded.

  • pu
    Expands to “public”
  • pr
    Expands to “private”
  • re
    Expands to “return”
  • fore
    Creates a for-each loop wih smart detection of collection classes to use.
  • Pm
    Creates a skeleton public method declaration.
  • pm
    Creates a skeleton private method declaration.
  • sout
    Expands to “System.out.println(“|”)”

If you never tried the code templates before, you should at least try the method declaration templates (pm and Pm). They are clever implemented and really convenient to use.

 

Leave a Reply

Your email address will not be published. Required fields are marked *