How to auto-remove trailing whitespace in Eclipse?
eclipse, whitespace
Solution
- Removing whitespace from the entire file being edited:
Preferences -> Java -> Editors -> Save Actions -> check Perform the selected actions on save -> check Additional actions -> click Configure.. -> go to Code organizing tab -> check Remove trailing whitespace -> select All lines.
- Removing whitespace only from the lines I changed:
Preferences -> Java -> Editor -> Save Actions -> check "Perform the selected actions on save -> check Format source code -> select Format edited lines.
Note it is not necessary to click Configure the formatter settings on the Formatter page - all the code styles automatically include removing trailing whitespace. At least I couldn't find a setting for this in Formatter configuration, and it worked out of the box for built-in Java Conventions, Eclipse, Eclipse 2.1 styles, as well as GoogleStyle.
When using this set-up, you obviously need to also turn off the solution to part 1 of the question.
Eclipse version checked: 4.5.2, 4.11
Problem
The question has two parts, one of which I already have the answer for. - How to auto-remove trailing whitespace from the entire file being edited? -> Answer: use the AnyEdit plugin, which can be set to do that on any save to the file. - How to auto-remove trailing whitespace only from the lines I changed? -> This I don't know and would appreciate any help.