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?
-
1Check the [following link](http://rakesh.sankar-b.com/2011/08/22/convert-tabs-to-spaces-in-eclipse-and-file-in-linux/) that I had written which contains the steps to convert tabs to spaces from Eclipse. – Rakesh Sankar Jul 10 '12 at 12:02
7 Answers
- Make sure you have your editor set to use spaces instead of tabs.
- Select all text and hit CMD + I on mac or CTRL + I on windows.
-
Eclipse is terrible. Even though I have my tabs now set to 4 positions and use spaces for tabs, when I use CTRL+I, it converts my tabs to 8 spaces. – lurker Jan 15 '23 at 20:28
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).

- 7,884
- 12
- 51
- 65
-
6need to be careful with eclipse formatter, it can really mess up declarations of arrays that have been formatted in a way to improve readability – Craig Angus Sep 30 '08 at 00:25
-
1Since readability can be so subjective, it is certainly worth at least trying to like formatting that can be automated. – Peter Hilton Sep 30 '08 at 07:58
-
1this answers committing to repository, but what about updating from repository? basically. if the repository is expected to use only spaces and no tabs, how would I update from it and have spaces converted to tabs. But then converted back to spaces on commit? this answers only half of the question – Peter Perháč May 03 '11 at 16:14
I use the AnyEdit plugin to auto-convert tabs to spaces on the save of a file. I also configure the base text editor (from which pretty much all the others derive) to insert spaces instead of tabs. This sounds redundant, but what it does is ensure that I don't insert any tabs, and any file that I edit that already has tabs will be converted as soon as I save it.
Tabs have no place in source code. If someone else looks at the file with their tab-stops set to a different value, they lose most alignment/formatting anyway.
(Of course, if you have Makefiles that you edit directly, you'll want to make sure their tabs are retained. But in my projects, if make is used at all the Makefile is derived from a different source, such as a Makefile.PL in Perl.)

- 5,525
- 4
- 31
- 37
A bit overkill, and only something to attempt with certain repository products that can handle it, but a hook script to call indent or astyle could do the trick. It'll format everyone's code the same way for every file, depending how you write the hook script, and it'd have to be pre-commit of course.

- 4,367
- 2
- 30
- 57
You may lose alignment/formatting by using tabs instead of spaces if and only if the tabs are not at the beginning of the line. Never use tabs insides lines, always use tabs at the front of lines. This allows you to use your editor to adjust to your desired indent level without impacting your co-workers view of the file. Challenge: Find an example where tabs at the front of the line loses alignment.

- 27
- 1
-
4Tabs at the front of the line loses alignment when multiple people/editors are working on the file and the file gets a mix of tabs and spaces... and people have their editors set to different tab widths (typically 4 and 8). – Craig McQueen Feb 21 '11 at 02:38
I use Kedit for just this thing. It also natively converts text files from Macintosh, UNIX and MS-Dos. Since it's an older editor, I use one of it's scripts to handle unicode files. You might also want to look at some of the other smart editors.

- 1,234
- 13
- 24
I use Eclipse for Java EE developer 4.6.0 Neon. I use http://marketplace.eclipse.org/content/anyedit-tools

- 46,709
- 59
- 215
- 313