Automatically remove trailing whitespace in Eclipse

by Thizzer

I have been working with Git for a while now. Today I encountered an error related to trailing whitespace in my files. Because most of the time I don't consciously add trailing whitespace to my lines I was hoping Eclipse would solve this for me, and it did!

I encountered the error with Git while trying to discard changes I had made:

 

'git apply' failed with code 1: <path to file> trailing whitespace

 

After some research I found out that Git and trailing whitespace don't play very well together resulting in confusing diffs. Codeimpossible has written a nice article on 'the evil of trailing whitespace' and its relation to git. 

 

Because most of the time I don't consciously add trailing whitespace to my lines I was hoping Eclipse would solve this for me, and it did! I also found some information on removing trailing whitespace using post-commit hooks, but for now the Eclipse solution is enough for me. You can remove the trailing whitespace on a per editor basis.

 

PHP

1. Go to Eclipse Preferences > PHP > Editor > Save Actions.

2. Enable the 'Remove trailing whitespace' option.

 

Java/Javascript

1. Go to Eclipse Preferences > Java > Editor > Save Actions.

2. Enable the Perform selected actions on save option.

2. Enable the Additional action option.

3. Click the Configure button next to the Additional actions box.

4. Select the Code Organizing tab.

5. Last but not least enable the 'Remove trailing whitespace' option.