Inconsistent indentation between tabs and spaces is one of the most persistent frustrations in software development, script drafting, and configuration file editing. When files containing hard tab characters (\t) are rendered across different text editors, web repositories, or terminal pagers, code alignment often breaks due to differing default tab-width interpretations. While you can configure modern text editors to replace tabs automatically, converting existing legacy codebases, log files, or text streams directly from the command line requires a dedicated, standard tool.
The GNU expand command is a core utility included in virtually every Linux distribution. It seamlessly converts tab characters into spaces while preserving the original column alignments. In this guide, we cover how to use expand in the Linux terminal to replace tabs with custom space widths, convert initial tabs only, and process shell pipelines efficiently.
In This Guide
- How the expand Command Works
- Basic Syntax and Default Tab Width
- Setting Custom Tab Widths (-t Option)
- Converting Leading Tabs Only (-i Option)
- Using Variable Tab Stop Lists for Tabular Data
- Integrating expand in Pipelines and Saving Output
- Batch Converting Multiple Files Safely
- Reversing the Process with unexpand
How the expand Command Works
Unlike a crude sed 's/\t/ /g' string substitution—which naively swaps every tab character for a static number of spaces regardless of current column position—expand respects tab stops. In traditional typesetting and terminal emulation, a tab character advances the cursor to the next predefined tab stop. If a word occupies three characters on a line with 8-character tab stops, pressing Tab advances the cursor by five spaces, not eight. The expand command calculates the exact number of spaces needed to reach the next tab stop, guaranteeing that tabular data and indented code blocks maintain their precise horizontal alignment.
Basic Syntax and Default Tab Width
The standard syntax for expand is simple:
expand [options] [file...]
By default, if no options are supplied, expand assumes standard 8-character tab stops. It reads the input file and writes the converted text directly to standard output (the terminal screen):
expand sample.txt
To inspect whether your file actually contains hard tabs before running expand, you can view invisible control characters using cat -A or cat -T:
cat -T sample.txt
Hard tabs will be displayed visually as ^I.
Setting Custom Tab Widths (-t Option)
While 8-space tabs were standard in legacy Unix terminals, modern programming conventions typically standardise on 4-space or 2-space indentation (such as in Python, JavaScript, and YAML configurations). Use the -t (or --tabs) flag followed by a single integer to define a uniform tab width:
To convert all tabs to 4 spaces:
expand -t 4 source_code.py
To convert all tabs to 2 spaces for web markup or configuration files:
expand -t 2 config.yaml
Converting Leading Tabs Only (-i Option)
In many files, tabs are used both at the beginning of a line for structural indentation and in the middle of a string literal or comment block. If you only want to convert the indentation tabs at the start of each line while preserving tab characters embedded within strings, combine -i (or --initial) with -t:
expand -i -t 4 script.sh
Any tab encountered after the first non-whitespace character on a line will be left untouched, preventing accidental formatting changes inside quoted text.
Using Variable Tab Stop Lists for Tabular Data
When dealing with fixed-width column exports—such as reports containing varying column lengths for IDs, names, and timestamps—a uniform tab width may not align columns cleanly. The -t option accepts an explicit, comma-separated list of column positions:
expand -t 10,25,40 data_table.txt
In this example:
- The first tab stop is placed at column 10.
- The second tab stop is placed at column 25.
- The third tab stop is placed at column 40.
Any tabs occurring beyond the final specified column will be replaced with a single space by default.
Integrating expand in Pipelines and Saving Output
Because expand reads from standard input when no filename is specified, it integrates seamlessly into standard Unix pipelines. For instance, you can retrieve command output, strip tabs, and pipe it directly to a pager or filtering tool:
git diff | expand -t 4 | less
To save the converted output to a new file, redirect the output using shell redirection:
expand -t 4 input.txt > output.txt
Warning: Do not attempt to redirect output back into the source file directly (e.g. expand -t 4 file.txt > file.txt), as the shell truncates the file before expand can read it. To overwrite files safely in place, use a temporary file or the sponge utility:
expand -t 4 file.txt | sponge file.txt
Batch Converting Multiple Files Safely
If you have a project directory containing dozens of source files that need to be standardised to 4-space indentation, you can combine find, expand, and sponge (from the moreutils package):
find src/ -type f -name "*.c" -exec sh -c 'expand -t 4 "$1" | sponge "$1"' _ {} \;
This command traverses the src/ directory, locates all C source files, converts tabs to 4 spaces, and updates each file in place without risking file truncation.
Reversing the Process with unexpand
If you ever encounter a scenario where spaces need to be converted back into tabs—for example, when working with Makefiles where leading indentation must strictly consist of genuine tab characters—Linux provides the complementary unexpand command:
unexpand -a -t 4 input.txt > output.txt
The -a (or --all) flag instructs unexpand to convert all qualifying sequences of spaces into tabs, rather than only leading whitespace.