Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto-remove trailing whitespace in Eclipse?

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.
like image 694
Jan Żankowski Avatar asked Jun 25 '09 11:06

Jan Żankowski


People also ask

How do you get rid of trailing white space?

Type M-x delete-trailing-whitespace to delete all trailing whitespace. This command deletes all extra spaces at the end of each line in the buffer, and all empty lines at the end of the buffer; to ignore the latter, change the variable delete-trailing-lines to nil .

How do you get rid of leading and trailing white spaces?

To remove leading and trailing spaces in Java, use the trim() method. This method returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.

How do you delete all leading and trailing spaces in all lines?

To do this, make sure the Home tab is active on the ribbon and click the “Show/Hide ¶“ button in the Paragraph section. Spaces are shown as dots. Select the lines that contain spaces you want to delete and center the lines by pressing Ctrl+E, or clicking the Center button in the Paragraph section of the Home tab.


2 Answers

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)

like image 132
gcastro Avatar answered Oct 30 '22 00:10

gcastro


  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
like image 35
Dan Berindei Avatar answered Oct 29 '22 22:10

Dan Berindei