ChatGPT解决这个技术问题 Extra ChatGPT

How to auto-remove trailing whitespace in Eclipse?

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.

I'd be happy if I could just convince Eclipse to stop adding trailing whitespace that I didn't type.
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.
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]
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!

z
zb226

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)


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.
Good for Java; good for C/C++ too; but not for Python in PyDev.
For Python in PyDev, I've made a feature request for this. See: sourceforge.net/tracker/…
FYI... Same concept for PHP. Preferences -> PHP -> Editor -> Save Actions.
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.
z
zb226

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

How do I define the format? Do I use the Formatter link or the Configure button?
This option appears in PDT for PHP as well, under Save Actions.
@VijayKotari Answer has been edited - no need to define a format.
M
M. A. Kishawy

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


This is the first answer that specifically advises looking under "Additional actions" for "Remove trailing whitespace." Thanks.
this only works for .java files, I'm usually working with dozen of extentions
Works for Eclipse 4.12
M
M. A. Kishawy

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


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.
There is a new option in PyDev Code Formatter: On save, only apply formatting in changed lines.
-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.
M
M. A. Kishawy

Do following:

Preferences -> Java -> Editor -> Save Actions

https://i.stack.imgur.com/C3ibC.png


K
Kena

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.


Thank you very much for this answer! So far it's the only way that I have found for Aptana plugin.
M
Malvineous

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.


R
Romain Linsolas

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


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.
a
anatoly techtonik

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


H
HeyMan

https://i.stack.imgur.com/n1a55.png


j
jcoffland

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>

f
fuweichin

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)

Find [\t ][\t ]*$ Replace with blank string Use Regular expressions Replace All

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


How about [\t ]+$ instead of [\t ][\t ]*$?
v
van

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.


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

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

It removes ^M characters that are trailing.