How to replace tabs by spaces in whole codebase?

I’ve stared working on a project on github, written in php, that have mixed spaces and tabs for indent, I’m using GNU/Linux so I thought that I just replace all tabs by spaces using bash. Here is the command I’ve used that just did that:


find . -type f -name '*.php' | while read file; do
     grep $'\t' $file > /dev/null && sed -ie 's/\t/    /g' $file
done

Explanation:

  • find . -type f -name '*.php' – this will search for all files with php extension, in current directory. I’ve used -type f to return only files since there where directories that end with php.
  • while read file; do ... done – while loop over files found by find, the filename will be in file variable
  • grep $'\t' $file > /dev/null – this will return true if file contain tabs
  • & & execute next command if previous is true
  • sed -ie 's/\t/ /g' $file – replace tabs by spaces in file inline (-i option)
  1. Leave a comment

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

%d bloggers like this: