score:155

Accepted answer
  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

score:0

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

it removes ^m characters that are trailing.

score:1

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.

score:4

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>

score:4

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

score:5

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

score:8

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

score:9

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

score:13

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.

score:20

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.

score:37

do following:

preferences -> java -> editor -> save actions

enter image description here

score:46

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

score:73

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

score:352

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)


Related Query

More Query from same tag