157

In eclipse you can click Ctrl+I at any line, and it'll automatically indent the line or group of lines according to the indentation scheme you chose in the settings.

I'm really missing this feature in Netbeans. Is there any equivalent feature?

I'm aware of Alt+Shift+F but it's not good enough. I want to indent a group of lines, and not all the file.

Hulk1991
  • 3,079
  • 13
  • 31
  • 46
Elazar Leibovich
  • 32,750
  • 33
  • 122
  • 169
  • 9
    Alt+Shift+F now reformats only a selection, not the whole file. – Josef Sábl Dec 09 '11 at 14:49
  • You can also set your key mappings to be the same as Eclipse with one setting in the Preferences. I do this when I'm using NetBeans because I'm primarily an Eclipse user. That way I don't have to remember two systems. – crowmagnumb May 21 '14 at 19:33
  • I've protected this question as I see many _thanks_ answer by low rep users on it. – Tushar Nov 17 '16 at 02:54

10 Answers10

204

Shortcut:

  • Windows: Alt+Shift+F
  • Mac OS X: Ctrl+Shift+F (note: it's Ctrl and not )

On using above shortcut, NetBeans indents your selection. If nothing's selected, it indents the whole file.

You can even format multiple files/folders at a time! In the Projects window/sidebar, if you select one or more folders or files and use the shortcut, NetBeans asks "Recursively format selected files and folders?". Pressing OK will recursively format all the selected files/folders.

Above shortcuts works on NetBeans from versions 7 to 12.

Manu Manjunath
  • 6,201
  • 3
  • 32
  • 31
  • I am a little late to the party but this shortcut does a lot more than just affect indentation. Out of the box, reformatting adds/removes linebreaks (for example on `/** **/` comments and near curly brackets `{}`). This is particularly troublesome when working with other people's code and all you want to do is have the indentation match what you find readable. – kwah Jun 29 '13 at 12:25
  • 1
    "Ctrl + Shift + F" works for me in my Mac with "NetBeans IDE 8.0.2". – arango_86 Aug 21 '15 at 09:15
101

Open Tools -> Options -> Keymap, then look for the action called "Re-indent current line or selection" and set whatever shortcut you want.

Koopakiller
  • 2,838
  • 3
  • 32
  • 47
varad
  • 1,606
  • 1
  • 14
  • 18
  • 1
    Still doesn't work with JavaFX, but eclipse JavaFX plugin have this defect as well, so I guess I just have to wait for JavaFX to mature. Thanks! – Elazar Leibovich Aug 24 '09 at 20:05
  • Tried it with a minified file (all code on one line) and won't indent anything: the whole file stays on one line. – Marco Marsala May 12 '16 at 06:27
31

Shift + Alt + F indents the whole file.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
Ensode
  • 311
  • 2
  • 3
11

Select the lines you want to reformat (indenting), then hit Alt+Shift+F. Only the selected lines will be reformatted.

OliCoder
  • 1,370
  • 10
  • 25
7

Ctrl+Shift+F will do a format of all the code in the page.

biegleux
  • 13,179
  • 11
  • 45
  • 52
Milhous
  • 14,473
  • 16
  • 63
  • 82
  • 2
    In the latest Netbeans (6.71) it's Alt+Shift+F and it doesn't work in javaFX mode. Eclipse CDT (for C/C++) has it working. It's still not good enough, as I sometimes want to indent only a few lines, and not all the code (for instance when fixing small portions of big file with bad identation. I don't want to change the indentation of all lines) – Elazar Leibovich Aug 21 '09 at 15:39
  • 2
    Ctrl+Shift+F opens the "Find in Projects" dialog box – Manu Manjunath Feb 18 '14 at 09:07
  • 3
    LoL, 'ctrl' + 'shift' + 'f' is shortcut for Find in All Files – Snopzer Jun 13 '16 at 07:34
7

Here's the complete procedure to auto-indent a file with Netbeans 8.

First step is to go to Tools -> Options and click on Editor button and Formatting tab as it is shown on the following image.

enter image description here

When you have set your formatting options, click the Apply button and OK. Note that my example is with C++ language, but this also apply for Java as well.

The second step is to CTRL + A on the file where you want to apply your new formatting setting. Then, ALT + SHIFT + F or click on the menu Source -> Format.

Hope this will help.

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
Gabriel L.
  • 4,678
  • 5
  • 25
  • 34
5

To format all the code in NetBeans, press Alt + Shift + F. If you want to indent lines, select the lines and press Alt + Shift + right arrow key, and to unindent, press Alt + Shift + left arrow key.

Mike G
  • 4,232
  • 9
  • 40
  • 66
Mohammed Saqib Rajput
  • 1,331
  • 1
  • 14
  • 22
4

If you want auto-indent just like Emacs does it on TAB, i.e. indent the current line and move the cursor to the first non-whitespace character, do this:

  1. Go to Tools -> Options -> Editor -> Macros
  2. Create a new macro and call it something like "tabindent"
  3. Insert the following macro code:

    reindent-line caret-line-first-column caret-begin-line

  4. Click "Set Shortcut" and press TAB

Elazar Leibovich
  • 32,750
  • 33
  • 122
  • 169
chenno
  • 41
  • 1
2

for Java NetBeans 7.1 and later, even in NetBeans 8.0 (That i´m currently using) and later, the shortcut is:

Alt+Shift+F

if you look into the KeyMap accessing from the menu: Tools -> Options -> Keymap, the "action" is Format defined with the Shortcut : Alt+Shift+F

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
2

I have netbeans 6.9.1 open right now and ALT+SHIFT+F indents only the lines you have selected.

If no lines are selected then it will indent the whole document you are in.

1 possibly unintended behavior is that if you have selected ONLY 1 line, it must be selected completely, otherwise it does nothing. But you don't have to completely select the last line of a group nor the first.

I expected it to indent only one line by just selecting the first couple of chars but didn't work, yea i know i am lazy as hell...

rciafardone
  • 125
  • 1
  • 2
  • 10
  • Thanks. Maybe it was fixed since I've written it, maybe I didn't notice and it was always OK. Good to know it's working (almost) as it should now. – Elazar Leibovich Apr 14 '11 at 08:51