Share this:

The Most Handy NetBeans Shortcuts For Ruby Development

ShortcutAs some of you know I’ve been digging more deeply into Ruby lately, using various libraries and in general having some Ruby fun. Well, throughout all this I’ve been using NetBeans as my IDE. It’s been interesting switching over from Eclipse, I’ve been feeling a little unproductive since I didn’t know any of the shortcuts. After a while it got particularly annoying so I decided to force myself to learn all the useful shortcuts, mostly by denying myself the use of the mouse as much as I could :). Many of the shortcuts that work for a Java in NetBeans don’t work for Ruby, but I did eventually settle on a list of the most useful ones (slowly starting to learn to use these instead of doing things the hard way). Anyways, I thought I’d share:

Code Editor-Related Shortcuts

  • Ctrl+Shift+Up – copy line under cursor up.
  • Ctrl+Shift+Down – copy line under cursor down.
  • Alt+Shift+Up – move line under cursor  up.
  • Alt+Shift+Down – move line under cursor  down.
  • Alt+Shift+Period – this one is used for selection, every time you press this one the selection widens to capture the next logical syntactic element. For example if you have your cursor over a word, pressing this ones would select the whole word, pressing again would select the whole line. If you’re inside a method definition, pressing it again would select the content of the method (between def .. end), once more would select the whole method. If you continue pressing it, you will eventually select the whole file. Very handy.
  • Alt+Shift+Comma – this one is the opposite of the one above, it will narrow the selection every time you press it.
  • Ctrl+/ – comment or uncomment line.
  • Ctrl+R – in-place rename entity under cursor. If it is a class, it will take you to the refactor dialog box. If it is a variable it will highlight all usages of that variable and attempt to change all of them to your new value (i.e. it is basically a rename refactor).
  • Ctrl+Q – go to last edited document/position. This will jump you to the last position you were editing before your current edit. If you were editing a different document before, it will jump you to that one. Keep pressing it to jump between positions you’ve been editing.
  • Ctrl+Shift+M – bookmark current line. This one is difficult to get used to, but if you want to remember a particular place for some reason, this is a handy one to know.
  • Ctrl+Shift+Period – cycle forward through bookmarks. Complements the one above.
  • Ctrl+Shift+Comma – cycle backwards through bookmarks. Complements the two above.
  • Alt+Enter – select quick fix. When the little light bulb appears offering a quick fix for a problem, you can use this one instead of clicking on it if your cursor is on the line.
  • Ctrl+B – go to declaration of symbol under caret. If it is a variable will jump to the first time the variable was declared. If it is a method name (i.e. method call) will jump to the definition of the method.
  • Alt+Shift+F – format code selection or file. If there is a selection, will format the selection, otherwise the whole file.
  • Ctrl+G – go to line. Will let you jump to a particular line number.
  • Ctrl+E – delete line under cursor. I love the ability to do this in Eclipse, so a really handy one to remember for NetBeans.
  • Ctrl+K – complete word by inserting word from buffer. This one is awesome! Start typing a word then press the shortcut – it will attempt to complete the word based on what else is currently in the file. If you press it repeatedly, it will cycle through all possible words in the file that might complete what you started typing. It does this very simply by scanning backwards through the file from your current position. It is usually correct the first time. Considering that IntelliSense is almost useless for Ruby development, this one is especially handy.
  • Ctrl+[ – jump to matching. This one is probably more useful for a language with brackets, but it can still be handy for Ruby to find a matching end for a do or an if.
  • Ctrl + Home – move cursor to start of file.
  • Ctrl + End – move cursor to end of file.
  • Alt + Shift + Right – move line one tab to the right. You can indent a line without having to move the cursor to the start of it.
  • Alt + Shift + Left – move line one tab to the left. Same as the one above, but lets you un-indent.
  • Shift + Enter – start new line below the current line and jump the cursor to the new line. Surprisingly handy.
  • do+Tab – insert do..end block. You just need to type do and then press TAB.
  • if+Tab – insert if..end block. Once again just type if and press TAB
  • ife+Tab – insert if..else..end block. Same story, type ife and press TAB.

IDE Related Shortcuts

  • Ctrl+Tab – scroll down through open files. If you have many files open in the IDE, this is a handy one to know. Pressing it once will pick the last open file you were in before the current one, keep pressing to scroll down through all the files currently open in the IDE.
  • Ctrl+Shift+Tab – scroll up through open files. The same as the one above but will let you scroll up through all the open files rather than scrolling down.
  • Ctrl+Shift+T – jump between tested file and test file. This one can be a little flaky but does work most of the time. If it doesn’t work for you, use one of the next two instead.
  • Ctrl+O – open a particular type. Search for a class by class name rather than file name.
  • Alt+Shift+O – open file by name. This one is complementary to the one above and will let you open a file rather than a type. Note that it is extremely slow if you have a large project (or several projects).
  • Ctrl+Shift+1 – select currently edited file in project view. I was so happy when I found this one, can be really annoying not to be able to jump to the file you’re editing (in project view).
  • Ctrl+Shift+2 – select currently edited file in files view. The brother of the one above, but lets you select the currently edited file in file view as opposed to project view.

If you have any other NetBeans shortcuts that would be particularly useful for Ruby development (i.e. I don’t just want any random ones, I know NetBeans has lots), please share and I’ll add them to the list.

Update: Here are some more handy shortcuts from the comments below (thanks to all the readers who shared their favorite ones)

  • Ctrl+1 – jump to files in project view
  • Ctrl+0 – jump back to editor (complements the one above)
  • Ctrl+Shift+V – pastes the code and formats it at the same time (very handy)
  • Ctrl+Shift+P – reformats/wraps comment lines to the designated right margin
  • Alt+Shift+F6 – run current test under cursor

Image by kathryn_rotondo

  • Rafał Lisowski

    I use Netbeans in every day work but some shortcuts was new to me.
    Very handy list, thank you.

    You mention do+Tab and etc. in fact this are code templates.
    Try def+Tab or rea+Tab in controller.
    To see templaes or add your own go to menu Tools > Options > Editor > Code Templates.

  • Alan Skorkin

    Yes, you’re of course correct, those are code templates, perhaps I should have separated them into a section of their own, but considering they come default with NetBeans I thought I’d just treat them as shortcuts.

    It is good to have the ability to define your own templates, but I don’t like being over reliant on stuff you define yourself in the IDE. As soon as you switch machines it’s a pain when those templates are not there (unless the templates are owned by the team and are not just your own stuff). This becomes especially evident when you’re doing a lot of pairing and are switching machines frequently.

  • rubytester

    Thanks, I also use:
    Ctrl+1 for jumping to files in project (like Ctrl+Shift+1 for current)
    Ctrl+0 for jumping back to editor
    Alt+~ configured it to open list of current editors to jump between them ( Ctrl+Shift+Tab a big cumbersome, you have to Tab to each file) with my setup I can open docs and then type the first chars to select it

    (and thanks: Shift+Enter and Ctrl+[ are new to me)

  • Chris Kutler

    Here are a couple more that I like to use

    Ctrl+Shift+V pastes the code and formats it at the same time.

    Ctrl+Shift+P reformats wraps comment lines to the designated right margin. It recognizes RDoc formats so it won’t mess with lists and such.

    • Alan Skorkin

      Those would definitely come in handy, thanks for sharing.

  • Anton

    alt + shift + F6 – run current test under cursor(spec in my spec file)

  • Alan Skorkin

    Ahh, that’s really good too if it works for specs as well, thanks.

  • brian k.

    ….Alt+Shift+O – open file by name. This one is complementary to the one above and will let you open a file rather than a type. Note that it is extremely slow if you have a large project (or several projects)…..

    Not anymore. Although the “Open File Fast” plugin takes about 70 secs to index each of my very large Rails projects each time I start the IDE, from that point on, it’s lightning-fast at the textmate-style “open by filename.” I usually only open it once per day, so this is a non-issue for me.

    I have many of my shortcuts remapped to mimic TM, so to me this is the CMD+T that has been missing. It works so well that I’ve actually got a couple of TM guys in the shop taking NB for a spin.

    • Alan Skorkin

      That looks like an awesome plugin, I will definitely be trying it out, thanks for sharing that.

  • song

    Alt-Insert for Rails Generator

  • ben

    so are they not documented in netbeans help ?. just asking because I used to use eclipse too. I have used netbeans only on a few occasions. Nowadays I am playing with Rebol, which I find is really cool. so I don’t need a sophisticated IDE.

    Just wondering why those guys at Netbeans did not document that ? Sometimes I am really frustrated with the open source products, because of their complete disregard for end users.
    I remember I did post a question on the netbeans forum, asking them how I could get help on a Ruby reserved word, just like in java. you position your cursor on a java word and righ click and select “show javadoc” . you know the ALT F1 on a java class for example.

    I never got a good answer and Never got it to work so I gave up on netbeans.

    I am not talking about code completion here.

    • Alan Skorkin

      All the shortcuts actually are documented in netbeans, unfortunately a lot of documentation is almost as bad as no documentation, things become really hard to find. Netbeans is in it’s own ways as good as eclipse, just need to get used to it.

  • Carl

    I use netbeans for php, but this is still a phenomenal list. Thank you for putting it together, you just helped me become more productive!

    A couple I use that I don’t think I read… (I assume they work for ruby development too)

    Select a block of text- then (tab/shift-tab) or (alt-shift-right/left) will indent/unindent the whole thing

    Tools->options->editor->code templates
    This has a bunch of shortcuts you can use by typing out a few letters and then hitting shift-space, and you can create your own easily

  • Sheth

    Thanks a lot! You’ve made a detailed explain.

  • Raf

    They’ve abandoned Ruby now and NetBeans 7 is missing the ruby add-on, so have learning of all this been useless?

  • Davor Cubranic

    Raf, while Ruby is not included in any official Netbeans download bundles, it is still available in the plugins catalogue and can be installed right from the IDE. See for step-by-step instructions.