Use sed to replace spaces with tabs from command line

Really useful code Rosuav turned me on to:

sed -i 's/^\(\t*\) /\1\t/' path/file.ext

Apparently sed is a relatively obscure UNIX program that works really well in editing streams, to be potentially piped to another program.

With the above code you will run it once for every level of indentation in the code. As written it replaces the number of spaces between the first part of the regular expression, s/^/(\t*\), and the second part, /\1\t/ with a single tab. Four spaces is a likely choice, depending on the code under consideration.

What Chris had me do was use git to monitor our progress like so:

sed -i 's/^\(\t*\) /\1\t/' path/file.ext
git add .
git status

And when it hasn’t modified any files, you’re done. Pretty cool. Really comes in handy with a space-syntax language like Python.

As we were going through, he also pointed out that, “Ya should be able ta just go up three times and return over and over until you’re done.”

Click click click boom click click click boom… love it.

This one liner will recursively replace each tab with four spaces:

find . -name '*.php' ! -type d -exec bash -c 'expand -t 4 "$0" > /tmp/e && mv /tmp/e "$0"' {} \;