1

I just realized after committing the CakePHP source to GitHub that they're now using tabs to indent code rather than four spaces. They also define this in the .editorconfig file, which I've changed to this:

root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

Is there a way to run through the entire source code and safely convert all tabs to four spaces for indentation? My reasoning is every developer on the repo uses four spaces and mixing and matching will cause the code to look out of place when looking at it on GitHub. And I'm just a fan of consistency :)

If I'm going down the home-brew way and writing my own script for this, I don't really mind what language although I'm more confident in PHP (not the best suited for the job, I know). Is this as simple as doing a preg_replace('~\t~', ' ', $fileText) on each file?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
James Dawson
  • 5,309
  • 20
  • 72
  • 126
  • Four spaces is the standard because it displays the same across IDEs - I'm really surprised that GitHub wouldn't follow that standard. Many IDEs have an auto format feature that would accomplish this for you. – crush Jan 30 '13 at 13:35
  • Yes to your regular expression, but if you use an IDE like Netbeans these operaitons should be built in and automatically expand tabs to spaces (if set to do so). Keep in mind though that tabs scales to the author preference, which spaces do not. – Daniel Jan 30 '13 at 13:38
  • @crush I think this is a Cake thing rather than GitHub, the source files have tabs rather than spaces which were in the previous verisons before 2.3.0 (I think). – James Dawson Jan 30 '13 at 13:42
  • @JamesDawson - It's certainly a cake thing AFAIK, all my projects keep tab indentations both in rep and in the online code browser. – Daniel Jan 30 '13 at 13:43
  • @JamesDawson CakePHP has always used tabs for indentation, not just since 2.3 – ADmad Jan 30 '13 at 14:53
  • "after committing the CakePHP source to GitHub that they're now using tabs to indent code rather than four spaces" => they do not use anything. github is fine either way. the only DISPLAY spaces instead of tabs (as tabs can be 8 spaces in "pre" formatting inside browsers which is usually too much). so its just a display thingy and can be disregarded. use tabs consistently as we all do and you will be fine. – mark Jan 30 '13 at 22:01
  • "use tabs consistently as we all do and you will be fine." Most people actually use 4 spaces, which is the preferred option as it's more consistent indentation over different systems. – James Dawson Jan 31 '13 at 22:01

3 Answers3

2

Try this in the directory you wish to execute it in:

find ./ -type f -exec sed -i 's/\t/XXXX/g' {} \;

That should replace the tabs with 4 spaces (if you replace the X's with spaces).

Adjust the space between t/ and /g with however many spaces you want…just get rid of the X's and put spaces in there.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
lobstahcrushah
  • 54
  • 3
  • 12
1

A straight replacement of tabs with spaces will result in misalignment when tabs follow space characters that encroach on that tab region.

A basic python script which makes use of the expandtabs() string method will result in code looking the same as when it was conceived. Example is for a tab space of 4:

#!/usr/bin/python
#
# convert source code or text with spaces, being careful to align text as it was conceived
# with the original tab space settings, which is defaulted to 4 spaces per tab.
#
# usage:
# ./tabs2spaces.py <file_to_convert>

import os
import sys

spaces_per_tab = 4

argc = len( sys.argv )

if argc < 2:
    print 'no file argument specified'

filename = sys.argv[ 1 ]
old_filename = 'old_' + filename

os.rename( filename, old_filename )

fn = open( filename, 'wb' )
fo = open( old_filename, 'r' )

for line in fo:
    fn.write( line.expandtabs( spaces_per_tab ) )

fn.close()
fo.close()
Rogue
  • 11
  • 1
0

Not sure if you have access to or already use Sublime Text 2, but it can automatically convert all the tabs to spaces for you:

How to replace four spaces with a tab in Sublime Text 2?

Community
  • 1
  • 1
lobstahcrushah
  • 54
  • 3
  • 12
  • I do have Sublime Text 2 and was aware of this feature but I have to do it for every file that I work on, I would like to swoop through every existing file and change it to spaces all at once to save me doing it over and over. – James Dawson Jan 30 '13 at 13:43