Removing tabs from programs in Unix

Tabs are evil. Partially because they are usually set to something like 8 spaces, and indents are typically 2 or 4 spaces. So how to automatically rid a program of tabs and replace them with spaces? One way is by using the Unix command expand, which expands tabs to spaces (specified as 4 in this case):

expand -t 4 input.c > output.c

Another way is using Perl (here replacing the tabs with 4 spaces):

perl -p -e 's/\t/    /g' input.c > output.c

This command replaces all tabs with 4 spaces. A third option is the use of sed (here replacing the tabs with 4 spaces):

sed $'s/\t/    /g' input.c > output.c

 All three of these methods deal with getting rid of tabs.

Advertisements

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