How can I convert tabs to spaces in every file of a directory?
How can I convert tabs to spaces in every file of a directory (possibly recursively)?
Also, is there a way of setting the number of spaces per tab?
Solution 1:
Simple replacement with sed
is okay but not the best possible solution. If there are "extra" spaces between the tabs they will still be there after substitution, so the margins will be ragged. Tabs expanded in the middle of lines will also not work correctly. In bash
, we can say instead
find . -name '*.java' ! -type d -exec bash -c 'expand -t 4 "$0" > /tmp/e && mv /tmp/e "$0"' {} \;
to apply expand
to every Java file in the current directory tree. Remove / replace the -name
argument if you're targeting some other file types. As one of the comments mentions, be very careful when removing -name
or using a weak, wildcard. You can easily clobber repository and other hidden files without intent. This is why the original answer included this:
You should always make a backup copy of the tree before trying something like this in case something goes wrong.
Solution 2:
Try the command line tool expand
.
expand -i -t 4 input | sponge output
where
-
-i
is used to expand only leading tabs on each line; -
-t 4
means that each tab will be converted to 4 whitespace chars (8 by default). -
sponge
is from themoreutils
package, and avoids clearing the input file. On macOS, the packagemoreutils
is available via Homebrew (brew install moreutils
) or MacPorts (sudo port install moreutils
).
Finally, you can use gexpand
on macOS, after installing coreutils
with Homebrew (brew install coreutils
) or MacPorts (sudo port install coreutils
).
Solution 3:
Warning: This will break your repo.
This will corrupt binary files, including those under
svn
,.git
! Read the comments before using!
find . -iname '*.java' -type f -exec sed -i.orig 's/\t/ /g' {} +
The original file is saved as [filename].orig
.
Replace '*.java' with the file ending of the file type you are looking for. This way you can prevent accidental corruption of binary files.
Downsides:
- Will replace tabs everywhere in a file.
- Will take a long time if you happen to have a 5GB SQL dump in this directory.