The question has two parts, one of which I already have the answer for.
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 .
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.
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.
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)
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With