432

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.
4
  • 103
    I'd be happy if I could just convince Eclipse to stop adding trailing whitespace that I didn't type.
    – divegeek
    Commented Aug 23, 2011 at 14:44
  • 6
    You don't need a plugin. Just use C++ -> Editor -> Save Actions. Also General -> Editors -> Text Editors -> Show whitespace characters (click on it for detailed settings) is extremely handy.
    – Trass3r
    Commented Jun 13, 2012 at 23:41
  • 4
    The default save action does not trim trailing whitespaces in Java Doc comments. Also, it's limited by the fact that it works on Java files only. We have a code review system which marks all trailing white spaces as errors. The AnyEdit is invaluable to take care of trailing whitespaces for all file types. Also, it might be useful to find what restrictions your company has for third part software. AnyEdit plugin has an EPL License (opensource.org/licenses/eclipse-1.0.php) [From here : andrei.gmxhome.de/anyedit/index.html] Commented Aug 1, 2012 at 12:29
  • 1
    I couldn't figure out why suddenly white spaces at end of lines are removed, until I saw your question and found out it was becuase of AnyEditPlugin. Thanks!
    – Tzafrir
    Commented Mar 21, 2016 at 7:40

14 Answers 14

356

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)

7
  • 2
    Thanks, gcastro - it is easier than AnyEdit. Unfortunately, it's for the entire file being edited .. still don't know how to do it for only the lines I changed - looks like it's probably impossible right now. Commented Aug 6, 2009 at 14:39
  • 8
    Good for Java; good for C/C++ too; but not for Python in PyDev. Commented Nov 24, 2009 at 5:37
  • 14
    For Python in PyDev, I've made a feature request for this. See: sourceforge.net/tracker/… Commented Jan 14, 2010 at 7:07
  • 5
    FYI... Same concept for PHP. Preferences -> PHP -> Editor -> Save Actions. Commented Jun 2, 2011 at 14:34
  • 1
    I don't know if this ever worked, but it certainly doesn't seem to work in the latest version of Eclipse (Kepler). You can run the formatter on edited lines only, but trimming whitespace is limited to all lines or ignoring empty lines - really annoying.
    – Vala
    Commented Feb 12, 2014 at 13:06
161
  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
3
  • How do I define the format? Do I use the Formatter link or the Configure button? Commented Feb 10, 2011 at 3:12
  • This option appears in PDT for PHP as well, under Save Actions.
    – thaddeusmt
    Commented Feb 28, 2011 at 5:50
  • 1
    @VijayKotari Answer has been edited - no need to define a format. Commented Apr 8, 2016 at 9:05
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

3
  • 4
    This is the first answer that specifically advises looking under "Additional actions" for "Remove trailing whitespace." Thanks.
    – DavidRR
    Commented Jul 26, 2012 at 14:17
  • 7
    this only works for .java files, I'm usually working with dozen of extentions
    – Heetola
    Commented Apr 21, 2015 at 8:45
  • Works for Eclipse 4.12 Commented Sep 1, 2019 at 9:48
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
3
  • 6
    This doesn't cover the question of stripping space only from edited lines. It's also a dangerous feature because it runs all of the Code Formatter options and an unchecked option is not the same as "ignore/don't use". ex: if "Use space after commas" is unchecked Code Formatter will remove those spaces instead of ignoring the option.
    – JCotton
    Commented Sep 14, 2011 at 0:10
  • 7
    There is a new option in PyDev Code Formatter: On save, only apply formatting in changed lines.
    – warvariuc
    Commented Jan 6, 2012 at 7:55
  • 3
    -1 for giving a too general answer—the question was just about white space; enabling auto code formatting means your source commits will start having unplanned stuff in them, causing a messy history because commits contain stuff that is unrelated to the commit title. Commented Nov 19, 2013 at 12:53
37

Do following:

Preferences -> Java -> Editor -> Save Actions

enter image description here

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.

1
  • Thank you very much for this answer! So far it's the only way that I have found for Aptana plugin. Commented Oct 18, 2010 at 9:58
14

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.

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

1
  • 4
    Yes, I'd like to keep the spaces on the lines not modified by me.. This is the default behavior of Intellij Idea (remove spaces on my lines, keep them on others), and I'd like to get that in Eclipse. Thanks for the answer. Commented Jun 25, 2009 at 11:51
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

5

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

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

1
  • 1
    How about [\t ]+$ instead of [\t ][\t ]*$? Commented Nov 7, 2015 at 14:52
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.

1
  • 7
    Since it is a save action it would be the lines changed between saves. Eclipse already keeps track of these lines for marking in the side bar.
    – starblue
    Commented Jun 25, 2009 at 12:14
0

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

It removes ^M characters that are trailing.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.