I would like to automatically convert between tabs and spaces for indentation when I commit/update code to/from our repository. I have found the AnyEdit plugin for eclipse, which can convert directories of files. Not bad for a start, but does anybody have more expierience on how to handle this? Or maybe know of an Ant script or something else?
Generally, a tab is the same width as 4 to 5 spaces provided the font being used equally sizes each character. For example, the Courier font's tab equals 5 spaces, whereas the Arial font is 11 spaces to each tab when the font size for both is set to 12.
To convert tabs in a text file to spaces, you can use the expand command. To convert each tab to a single space character, use the -t 1 option.
On the HOME tab on the Ribbon, choose Replace from the Editing group (or press [Ctrl]+H) to open the Find And Replace dialog box. In the Find What field, enter one space character and the following characters, exactly as shown in red: {2,} In the Replace With field, enter ^t. Click the More button.
Why not just use the code formatter and/or cleanup function? It has settings that take care of that stuff for you. You can even have it run automatically on save.
Edit: As Peter Perháč points out in the comments, this only answers half the question. I don't have any practical experience, but you could try the Maven Eclipse Format Plugin to format from a Maven build.
Unfortunately, that's Maven only, and I know of no light-weight command line formatter. But if you happen to use Maven, you can bind the format goal to the proper phase, and if you set Eclipse to auto-build, it would format on update.
Depending on the SCM tool (git, svn, etc), you could also create a hook that runs the build (but it might be a bit too heavy-weight for that).
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