[java] How do I autoindent in Netbeans?

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.

This question is related to java netbeans ide indentation

The answer is


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


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


Shift + Alt + F indents the whole file.


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


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


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.


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.


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


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.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to netbeans

Can't create project on Netbeans 8.2 I'm getting favicon.ico error Cannot find java. Please use the --jdkhome switch Netbeans 8.0.2 The module has not been deployed Error starting Tomcat from NetBeans - '127.0.0.1*' is not recognized as an internal or external command Cannot start GlassFish 4.1 from within Netbeans 8.0.1 Service area javac: invalid target release: 1.8 connecting MySQL server to NetBeans Starting of Tomcat failed from Netbeans Display Records From MySQL Database using JTable in Java

Examples related to ide

How can I view the Git history in Visual Studio Code? How to ignore a particular directory or file for tslint? How do I completely rename an Xcode project (i.e. inclusive of folders)? Where is the visual studio HTML Designer? How to disable gradle 'offline mode' in android studio? Android studio Error "Unsupported Modules Detected: Compilation is not supported for following modules" Android Studio was unable to find a valid Jvm (Related to MAC OS) QtCreator: No valid kits found Difference between WebStorm and PHPStorm package android.support.v4.app does not exist ; in Android studio 0.8

Examples related to indentation

How to indent/format a selection of code in Visual Studio Code with Ctrl + Shift + F Python IndentationError unindent does not match any outer indentation level How to change indentation mode in Atom? How to set HTML Auto Indent format on Sublime Text 3? "Expected an indented block" error? Indent starting from the second line of a paragraph with CSS Indentation Error in Python How to fix/convert space indentation in Sublime Text? How to make the tab character 4 spaces instead of 8 spaces in nano? IndentationError: unexpected unindent WHY?