Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can NetBeans remove trailing whitespace and change tabs to spaces on save?

Tags:

netbeans

I just started to use NetBeans, and am trying to work out how to:

  1. Make it so that trailing whitespace is removed on save. I know that you can invoke it manually, but it’s annoying if you forget. Can it be done automatically on save? I want this done because it keeps the code neat, because it’s really annoying when you’re moving through code and the text cursor goes all over the place because of trailing whitespace you don’t see, and because trailing whitespace has unintended effects on HTML layouts and other stuff.

  2. Make it so that tabs are automatically made into 4 spaces while you’re typing rather than converting on save, or, worse, only when manually invoked.

like image 911
cappuccino Avatar asked Nov 07 '10 21:11

cappuccino


People also ask

How do you get rid of trailing white space?

Type M-x delete-trailing-whitespace to delete all trailing whitespace. This command deletes all extra spaces at the end of each line in the buffer, and all empty lines at the end of the buffer; to ignore the latter, change the variable delete-trailing-lines to nil .

How do you get rid of leading and trailing white spaces?

To remove leading and trailing spaces in Java, use the trim() method. This method returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.

Why is trailing whitespace bad?

Some editors automatically remove trailing whitespace, some don't. This creates diff noise and can cause merge conflicts. You should coordinate with the people you're working with (colleagues, open-source crowd) what strategy everybody is using and make sure you all use the same strategy.

Which function of the string will remove leading and trailing spaces?

The STRIP function is similar to the TRIM function. It removes both the leading and trailing spaces from a character string.


1 Answers

For the first question, NetBeans 7.0+ has now a built-in solution. You can find it in:

On NetBeans 7:

Tools > Options > Editor > General > When saving files > Remove Trailing Whitespace: Never / Always / For Modified Lines Only

On NetBeans 8 / 9:

Tools > Options > Editor > On Save > Remove Trailing Whitespace From: None / All Lines / Modified Lines Only

like image 188
Gregoire Avatar answered Oct 02 '22 05:10

Gregoire