Untabify from the Linux command line


Every now and then, we get some source file that has those nasty relics from a distant path, namely tabs instead of spaces to make indentation. Of course, you can load the affected files one by one in your favorite editor and provided that you can specify some kind of regular expression in a global search and replace, you will be able to replace them.

But programmers are lazy so there must be an automated way of handling this task:

find . \( -name "*.?pp" -o -name "*.h" \) \
    -exec bash -c 'expand -i -t 4 "$0" > "$0.detab" && mv -v "$0.detab" "$0"' {} \;

This finds any C++ file (whether it ends in .cpp, .hpp or .h ; adding .C is left to the reader).

For any file, it runs expand on it to transform leading tabs into spaces (1 tab = 4 spaces).

To replace anywhere in the text, not just at line start, remove the -i.

 

Les commentaires sont fermés.