[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 width” settings enter 4 mark “insert spaces for tabs” checkbox.
Happy Coding with proper Indenting.
About these ads
Leave a comment

6 Comments

  1. Arafin Rubab

     /  January 23, 2012

    Much appreciated for the information and share!

    Reply
  2. This managed to mess up my git repository. Does that command also touch things inside the .git folders?

    Reply
    • Yes indeed, it will find any file inside the current directory (“.”)

      To exclude any files inside hidden folders (dot folders), try the following command:
      find ./ -type f \( ! -regex ‘.*/\..*’ \) -exec sed -i ‘s/\t/XXXX/g’ {} \;

      Reference:
      http://superuser.com/a/152961

      Reply
  3. AJMER SINGH

     /  November 22, 2012

    VERY NICE TAB

    Reply
  4. Phil Hord

     /  January 8, 2014

    you might also try ‘expand’. See ‘man expand’.

    Reply
  1. [TIPS] ECLIPSE – Associate an Editor to any File Type or File Extension « SHAREsome & learnMORE – Rakesh Sankar

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 104 other followers

%d bloggers like this: