Objective: Convert a text file with tabs to spaces on Linux.
There are various tools to convert tabs to spaces. One of the them is the expand
tool.
1 |
$ expand input.txt > output.txt |
To convert a single tab to 4 spaces, use the following syntax.
1 |
$ expand -t 4 input.txt > output.txt |
If you are only interested in replacing the initial tab-based indents with spaces in your source code files and not convert any tabs defined in strings, use expand
with “-i” option.
1 |
$ expand -i -t 4 input.c > output.c |
To convert a tab into 4 spaces using sed
, use the following syntax.
1 |
$ sed -e 's/\t/ /g' input.txt > output.txt |
If you are editing a file in vim, use :retab
command.
1 2 |
:set tabstop=4 :retab |