Oracle Blog

Java and Java Embedded

Beautifying NetBeans Code

One of the first things I used to do when opening a file for editing is to beautify it. Beautifying (also called auto-indenting) is the process of indenting the lines of code in a file "properly", where properly means "to my liking". I say this because everyone has their own opinion of proper indenting, number of indentation spaces, using tabs or whitespace, putting the curly bracket at the end of a declaration line or at the beginning of the next line, etc. Of course, beautifying ends up causing a bunch of meaningless diffs if you diff the file with a previous "un-beautified" version, but it makes the file much more readable for me (and therefore, I'm more productive).

In Emacs, I use:

Ctrl X, H, Esc, Ctrl \\

'Ctrl X, H' highlights the entire file and 'Ctrl \\' beautifies it.

NetBeans supports the same feature. You don't need to highlight any code if you want to beautify the entire file. To beautify a subset of the file, you can hightlight the text you want using standard keys. To beautify the text hit 'Ctrl Shift F' (you can also hit the right mouse button and choose 'Reformat Code'). This will beautify your code according to your settings in the indentation engine used for the type of file your beautifying.