How to auto-remove trailing whitespace in Eclipse?

EclipseWhitespace

Eclipse Problem Overview


The question has two parts, one of which I already have the answer for.

  1. 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.
  2. How to auto-remove trailing whitespace only from the lines I changed? -> This I don't know and would appreciate any help.

Eclipse Solutions


Solution 1 - Eclipse

I assume your questions is with regards to Java code. If that's the case, you don't actually need any extra plugins to accomplish 1). You can just go to Preferences -> Java -> Editor -> Save Actions and configure it to remove trailing whitespace.

By the sounds of it you also want to make this a team-wide setting, right? To make life easier and avoid having to remember setting it up every time you have a new workspace you can set the save action as a project specific preference that gets stored into your SCM along with the code.

In order to do that right-click on your project and go to Properties -> Java Editor -> Save Actions. From there you can enable project specific settings and configure it to remove trailing whitespace (among other useful things).

NB: This option has been removed in Eclipse Kepler (4.3) and following releases.

NB #2: The option seems to be back in Eclipse Luna - Luna Service Release 1a (4.4.1)

Solution 2 - Eclipse

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

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

Solution 3 - Eclipse

You don't need any plugin to do so. For instance, if you code JAVA, you can erase trailing whitespaces configuring save actions:

Eclipse 3.6

Preferences -> Java -> Editors -> Save Actions -> Check Perform the selected actions on save -> Check Additional actions -> Click the Configure.. button.

In the Code organizing tab, check Remove trailing whitespace

Solution 4 - Eclipse

PyDev can do it by either Ctrl+Shift+F if you have code formatter option set to do it, or by during saving:

Eclipse -> Window -> Preferences -> PyDev -> Editor -> Code Style -> Code Formatter:

I use at least these:

  • Auto format before saving
  • Right trim lines?
  • Add new line at end of file

Solution 5 - Eclipse

Do following:

Preferences -> Java -> Editor -> Save Actions

enter image description here

Solution 6 - Eclipse

You can map a key in Eclipse to manually remove trailing whitespaces in the whole file, but only on request instead of automatically at save. (Preference/Keys and then map a set of keys to File/Remove Trailing Whitespace) This can be useful if you want to sanitize all new files, but keep legacy code untouched.

Another strategy is to activate visual display of whitespace, so at least you'll know when you're adding some trailing whitespace. As far as I know, there's no way to display only trailing whitespace though, but I'll be glad to be proved wrong.

Solution 7 - Eclipse

In a pinch, for those editors that don't support removal of trailing whitespace at all (e.g. the XML editor), you can remove it from all lines by doing a find and replace, enabling regular expressions, then finding "[\t ]+$" and replacing it with "" (blank). There's probably a better regex to do that but it works for me without needing to install AnyEdit.

Solution 8 - Eclipse

I would say AnyEdit too. It does not provide this specific functionalities. However, if you and your team use the AnyEdit features at each save actions, then when you open a file, it must not have any trailing whitespace.

So, if you modify this file, and if you add new trailing spaces, then during the save operation, AnyEdit will remove only these new spaces, as they are the only trailing spaces in this file.

If, for some reasons, you need to keep the trailing spaces on the lines that were not modified by you, then I have no answer for you, and I am not sure this kind of feature exists in any Eclipse plugin...

Solution 9 - Eclipse

It is impossible to do it in Eclipse in generic way right now, but it can be changed given with basic Java knowledge and some free time to add basic support for this https://bugs.eclipse.org/bugs/show_bug.cgi?id=180349

The dependent issue: https://bugs.eclipse.org/bugs/show_bug.cgi?id=311173

Solution 10 - Eclipse

For php there is also an option:enter image description here

Solution 11 - Eclipse

There is a really easy way to do this with sed, the Unix command line tool. You could probably create a macro in Eclipse to run this:

sed -i 's/[[:space:]]*$//' <filename>

Solution 12 - Eclipse

As @Malvineous said, It's not professional but a work-around to use the Find/Replace method to remove trailing space (below including tab U+0009 and whitespace U+0020).
Just press Ctrl + F (or command + F)

  1. Find [\t ][\t ]*$
  2. Replace with blank string
  3. Use Regular expressions
  4. Replace All

extra:
For removing leading space, find ^[\t ][\t ]* instead of [\t ][\t ]*$
For removing blank lines, find ^\s*$\r?\n

Solution 13 - Eclipse

I am not aware of any solution for the second part of your question. The reason is that it is not clear how to define I changed. Changed when? Just between 2 saves or between commits... Basically - forget it.

I assume you would like to stick to some guideline, but do not touch the rest of the code. But the guideline should be used overall, and not for bites and pieces. So my suggestion is - change all the code to the guideline: it is once-off operation, but make sure that all your developers have the same plugin (AnyEdit) with the same settings for the project.

Solution 14 - Eclipse

I used this command for git: git config --global core.whitespace cr-at-eol

It removes ^M characters that are trailing.

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
QuestionJan ŻankowskiView Question on Stackoverflow
Solution 1 - EclipsegcastroView Answer on Stackoverflow
Solution 2 - EclipseDan BerindeiView Answer on Stackoverflow
Solution 3 - EclipseDavid MorabitoView Answer on Stackoverflow
Solution 4 - EclipseCianticView Answer on Stackoverflow
Solution 5 - EclipseSumit SinghView Answer on Stackoverflow
Solution 6 - EclipseKenaView Answer on Stackoverflow
Solution 7 - EclipseMalvineousView Answer on Stackoverflow
Solution 8 - EclipseRomain LinsolasView Answer on Stackoverflow
Solution 9 - Eclipseanatoly techtonikView Answer on Stackoverflow
Solution 10 - EclipseHeyManView Answer on Stackoverflow
Solution 11 - EclipsejcofflandView Answer on Stackoverflow
Solution 12 - EclipsefuweichinView Answer on Stackoverflow
Solution 13 - EclipsevanView Answer on Stackoverflow
Solution 14 - Eclipseuser7577563View Answer on Stackoverflow