$linuxjunkies
>

expand(1)

Convert tabs to spaces in text files.

UbuntuDebianFedoraArch

Synopsis

expand [OPTION]... [FILE]...

Description

The expand command reads files (or standard input) and writes them to standard output with tab characters converted to spaces. By default, tabs are expanded to the next multiple of 8 columns, matching the standard tab stop positions.

This is useful for preparing text files for display on systems that don't handle tabs consistently, or for ensuring consistent formatting across different environments.

Common options

FlagWhat it does
-t NUMSet tab stops to every NUM columns (default: 8)
-t LISTSet custom tab stops (e.g., -t 4,8,12 or -t 4,+4)
-i, --initialOnly expand tabs at the beginning of lines
-a, --allExpand all tabs, not just leading tabs (default behavior)
--helpDisplay help message and exit
--versionOutput version information and exit

Examples

Convert all tabs to spaces (8-column tabs) and print to stdout

expand file.txt

Expand tabs to 4-column spacing and save to a new file

expand -t 4 file.txt > output.txt

Pipe file through expand with 2-column tab stops

cat file.txt | expand -t 2

Only expand leading tabs (indentation) using 4-column stops

expand -i -t 4 script.sh

Set custom tab stop positions at columns 4, 8, 12, and 16

expand -t 4,8,12,16 data.txt

Set first tab stop at column 4, then every 4 columns thereafter

expand -t 4,+4 file.txt

Related commands