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

Netbeans

Netbeans Problem Overview


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.

Netbeans Solutions


Solution 1 - Netbeans

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

Solution 2 - Netbeans

[Original] For your first question, how to set NetBeans to remove trailing whitespace, the answer is that there is no setting (at the moment) to control that. It happens automatically, but not to every line. Whitespace is only removed from the lines you edit (or from the entire file if invoked manually). See Petr Pisl's blog entry titled "Removing trailing spaces" for a more complete explaination. (Note that even though he is blogging about NetBeans' PHP features, this particular feature applies to the basic editor shared by all NetBeans supported languages.)

[Update] This feature has been made available as of version 7.0, as pointed out by others in comments and answers below.

For your second question, set the editor to automatically make tabs into spaces as you type, you can find that setting in the Editor Formatting options:

  1. Click on the Tools menu, and choose Options
  2. Click on Editor in the top row of icons
  3. Click on the Formatting tab
  4. In the Language field, ensure that All Languages is selected
  5. In the Category field, ensure that Tabs And Indents is selected
  6. Check the box for Expand Tabs to Spaces
  7. For each additional language that is available in the Language field, check that either Override Global Options is not checked, or that Expand Tabs to Spaces is also checked for that language.
  8. Click OK

That will make sure that when you hit the tab key the specified number of spaces is inserted rather than a tab character. As above with whitespace removal, any existing tabs in the file are left as-is unless you invoke the manual command to change the entire file. (Moreover, even if you edit a line with tabs on it--leaving the tabs in place--the editor will not convert them to spaces. Only new instances of hitting the TAB key are controlled by this option.)

Solution 3 - Netbeans

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

This is as of Netbeans 7.3, according to Webmut.

Solution 4 - Netbeans

For netbeans 7.4+

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

Mac - Netbeans > preferences > Options > Editor > On Save > Remove Trailing Whitespace From: None / All Lines / Modified Lines Only

Also you can set this as language specific. You can also set reformat while saving.

Solution 5 - Netbeans

if you want to remove all trailing spaces in the file manually ONLY ONCE, you can invoke action 'Remove Trailing Spaces' from Source menu manually.

> Source -> Remove Trailing Spaces

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncappuccinoView Question on Stackoverflow
Solution 1 - NetbeansGregoireView Answer on Stackoverflow
Solution 2 - NetbeansRobertBView Answer on Stackoverflow
Solution 3 - NetbeansjameshfisherView Answer on Stackoverflow
Solution 4 - NetbeansBiswajit MajiView Answer on Stackoverflow
Solution 5 - NetbeansEugene KaurovView Answer on Stackoverflow