[HOW TO] Convert tabs to spaces in Eclipse and File (in Linux)

Tux, the Linux penguin

This is a quick tip that helped me save a lot of time – this is the reason why I love LINUX because of it’s simplicity.

I was working on a legacy code (referred as old-age code – a simple procedural coding style) for a client. In general, coding standard suggests you to have spaces instead of tabs to make it look better in all kinds of IDE, we (generally every developer) uses tab key a lot (to make our life easier) thinking it gives us the space but it inserts an actual tab (which usually jobs off from one end to the other). This legacy code was filled with tabs which looked pretty ugly in our code-review tool, though it looks perfect in the IDE, when you try to open it in Linux the favorite VI editor it looks ugly YUCK it is.

HUH, initially when my folks reported me I was like yeah go ahead and change it – what’s a big deal. But then when I came to know it not just one file but a lot (yes a lot) – it was almost more than 100 files, which had tabs instead of spaces (not GOOD – yeah I know).

But don’t worry, LINUX/ECLIPSE (open-source editor) saves a your time. I cracked my mind a little bit and came up with the following command which converts all of my tabs into spaces (4 spaces for one tab – as per the standard).

$ find ./ -type f -exec sed -i 's/\t/XXXX/g' {} \;
To exclude any files inside hidden folders (dot folders), try the following command:
find ./ -type f \( ! -regex '.*/\..*' \) -exec sed -i 's/\t/XXXX/g' {} \;
Adjust the number of spaces you want in the sed expression (replace X by space):
‘s/\t/XX/g’ for 2spaces,
‘s/\t/XXXX/g’ for 4 spaces

Yes, the power of CLI (Command Line Interface) – that’s a hell a lot of time saved.

BTW, we use Eclipse IDE for most of the projects, use the following steps to have (4) spaces when you press “tab” key in Eclipse IDE.

  1. Window > Preferences > PHP (General) > Code Style (Editor) > Formatter
  2. “Formatter”(name of tab). Select “Spaces” from the drop-down & choose 4 for “Indentation Size” field.
  3. Window > Preferences > General > Editors > Text Editors
  4. “Text Editors”(name of tab). Under “Displayed tab widthsettings entermark “insert spaces for tabs” checkbox.


In eclipse (in general, most of the IDE) has a file-type (.extension, ex: .php, .java, .rb, .js, etc.) associated with an editor, please ensure that you modify the above settings in those editors.

Happy Coding with proper Indenting.
[HOW TO] Convert tabs to spaces in Eclipse and File (in Linux)

11 thoughts on “[HOW TO] Convert tabs to spaces in Eclipse and File (in Linux)

  1. […] Command to Convert Tabs to Spaces inside any file in LINUX (rakesh.sankar-b.com) Advertisement GA_googleAddAttr("AdOpt", "0"); GA_googleAddAttr("Origin", "other"); GA_googleAddAttr("LangId", "1"); GA_googleAddAttr("Autotag", "technology"); GA_googleAddAttr("Autotag", "books"); GA_googleAddAttr("Tag", "eclipse-2"); GA_googleAddAttr("Tag", "tips"); GA_googleAddAttr("Tag", "eclipse"); GA_googleAddAttr("Tag", "editor"); GA_googleAddAttr("Tag", "extension"); GA_googleAddAttr("Tag", "tips-2"); GA_googleFillSlot("wpcom_below_post"); SHAREsome:Like this:LikeBe the first to like this post. […]

  2. Morti says:

    I could get Eclipse inserting 4 spaces for a tab but when I do ctrl + A and then ctrl + I (to select everything and indent the whole file properly) it converts all sets of 4 spaces back to tabs. Do you know how can I configure Eclipse so the ctrl + I also inserts spaces?
    Thanks in advance.

    1. This looks like the tab settings were not complete at some level. Let me throw some questions and figure out where you could have missed the settings.

      Did you change the settings in General >> Editors?
      Did you change the settings to the programming language specific Editors?
      – In case of PHP, it should be in Window > Preferences > General > Editors > Text Editors
      – In case of Java, it should be in Window->Preferences->Java->Code Style->Formatter->Edit->Indentation
      – Etc.

      For the shortcut to work you need to ensure that the tabs are converted to spaces for editors and the editors vary with the file associations, like .php will have its own editor, so far .java, .html, etc.

      Let me know. Hope it helps.

      1. Morti says:

        It worked! Now Ctrl + I inserts sets of 4 spaces in my .java files! Thanks for your help!

  3. Bill Kress says:

    This doesn’t look like it will work quite right. the problem is that “a\tb” should convert the tab into 3 spaces instead of 4. Every tab used to align columns will be out of whack.
    Another strangeness I encountered deailing with this: Eclipse seems to always copy the previous line to determine if it should use tabs or not–regardless of the settings! So if you have “\t \t” and you hit tab on the line below, you won’t get 12 spaces, you’ll get the same tab, 4 spaces, tab as the line above.
    The one solution that worked pretty well–made an external launcher in Eclipse to launch the current file in “Notepad++”, then I can tell Notepad++ to convert spaces to tabs–it does this correctly with a variable number of spaces depending on what it needs to get to the next correct tabstop.
    I was really surprised at how strangely Eclipse acted with this whole thing. It’s fine as long as you are set to use tabs (even if it’s tabs and spaces intermixed), but once I changed it to use spaces, tabs seem to confuse eclipse quite impressively.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s