Commands

Convert Tabs to Spaces in Linux Terminal With Expand Command

This tutorial teaches you to convert the tabs into spaces in Linux command line from the programming perspective.

Abhishek Prakash
Abhishek Prakash

Table of Contents

The debate over the use of tabs and spaces in the programming is a never ending one.

While you may prefer using tabs all the time, chances are that your coding guideline suggest using spaces.

But if you have already used tabs everywhere into your program and you need to convert those tabs into spaces so that the reviewer allows your code, you are at the right place.

In this tutorial, I’ll show you how to convert the tabs into spaces in a text file in Linux command line.

Using expand command to convert tabs into spaces in Linux command line

I am using this sample text file which is a simple C++ program for checking odd and even number. This is the content of the file:

Sample code with tab indentation

You can download this file from here if you want to practice the command while following this tutorial.

Check if your text file has tabs

There are several ways of doing it. The simplest method I find is by using the cat command.

You can use the cat command with option -T and will display all the tabs as ^I on the screen (stdout).

cat -T tab_file

You can see the location of the tabs in the file.

#include
using namespace std;
int main()
{
^Iint n;
^Icout << “Enter a number:^I“;
^Icin >> n;
^I
^Iif (n%2 == 0) {
^I^Icout<<n<< “is an even Number”;
^I}
^Ielse
^I^Icout<<n<< “is an odd Number”;
^Ireturn 0;
}
See the tabs in a file
File with tabs

Convert tabs into spaces with expand command

If you use expand command on a file, it will convert all the tabs into a block of 8 spaces and will display the output on the screen.

expand tab_file

But that’s not very convenient, is it? You will hardly see the changes here. A better idea would be to save the output to another file.

expand tab_file > space_file

Now if you see the file using the cat command, you won’t find any tabs anymore.

All tabs converted into spaces
All tabs converted into 8 spaces (default)

Reduce the number of spaces

As I mentioned in the previous section, by default, a tab is equal to 8 spaces. That would look super weird if your code has such huge indentation.

The good thing is that you can change the default space size with the -t option.

expand -t N tab_file

For example, if you have to change each tab into 2 spaces, you can use the expand command like this:

expand -t2 tab_file > space_file
Tabs converted into spaces with reduced size
All tabs converted into 2 spaces

Convert only leading tabs into spaces

Often in the programs, you would only want to convert the leading tabs i.e. the tabs at the beginning of the line. You don’t want to touch the tabs in between the lines that are actually part of the code.

Expand provides this option as well. If you want to only convert the leading tabs into spaces, use the –i option.

If we continue the previous example, here is what the command would look like:

expand -t2 -i tab_file > space_file
Convert only leading tabs into spaces
Only leading tabs converted into spaces

Convert tabs into spaces and save it to the original file

In all the above examples, you have saved the converted file into a new file. But if your aim is to clean your code by convert the tabs into spaces of the existing program files, you would want the output to be saved in the original file itself.

To do that, you can use the sponge command. Sponge ‘soaks up’ the entire standard input before writing it to output. This is extremely useful when you are trying to change and save the same file.

Now, the sponge command may not be available on your system. You’ll have to install the moreutils package. It should be available

On Ubuntu/Debian based distributions, you can use this command to install moreutils:

sudo apt install moreutils

Once installed, you can use it in the following function:

expand -t2 -i tab_file | sponge tab_file

Convert tabs into spaces in all the matching files in a directory

Till now whatever you learned was applicable to a single file. But if you have a project that has several program files and you want to convert the tabs into spaces in all of them, you’ll have to be a bit more smart here.

What we have learned so far can be combined with the magnificent find and exec commands.

find . -name '*.cpp' -type f -exec bash -c 'expand -t 4 "$0" | sponge "$0"' {} \;

The above command finds all the files ending with extension cpp, pass these files to the expand command and the expand command writes the output to the original files with the help of sponge command.

You might think using sed command would have been easier at this point but that’s entirely your choice.



Join the conversation.