LinuxCommandLibrary

expand

Convert tabs to spaces

TLDR

Convert tabs in each file to spaces, writing to stdout

$ expand [path/to/file]
copy

Convert tabs to spaces, reading from stdin
$ expand
copy

Do not convert tabs after non blanks
$ expand [[-i|--initial]] [path/to/file]
copy

Have tabs a certain number of characters apart, not 8
$ expand [[-t|--tabs]] [number] [path/to/file]
copy

Use a comma separated list of explicit tab positions
$ expand [[-t|--tabs]] [1,4,6]
copy

SYNOPSIS

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

PARAMETERS

-i, --initial
    convert only leading tabs, ignore tabs after non-spaces

-t N, --tabs=N
    set tab stops every N columns instead of 8 (implies -i)

-t LIST, --tabs=LIST
    use comma-separated LIST for exact tab stop positions (implies -i)

--help
    display this help and exit

--version
    output version information and exit

DESCRIPTION

The expand command converts tab characters in input files or standard input to spaces, ensuring the horizontal position of characters remains unchanged. By default, tabs are expanded assuming tab stops every 8 columns. This utility is essential for normalizing text spacing, especially when preparing files for tools intolerant of tabs or for consistent display across environments.

Key behaviors include: reading from one or more FILEs or stdin if none specified, writing to stdout, and handling multiple files by processing each separately. Custom tab widths or positions can be set, and an option limits expansion to leading tabs only. It preserves newlines and non-tab whitespace, making it safe for scripts and data processing pipelines.

Common use cases involve formatting source code, logs, or reports for fixed-width displays or further manipulation. Unlike simple substitutions, expand calculates spaces dynamically based on position, avoiding misalignment.

CAVEATS

Expanding many tabs can significantly increase line lengths, potentially exceeding buffer limits in some tools. Does not handle multibyte characters perfectly in older implementations.

DEFAULT BEHAVIOR

Tabs expand to reach next multiple of 8 columns; e.g., tab at column 1 becomes 7 spaces.

EXIT STATUS

0 on success, >0 if error (e.g., read/write failure).

HISTORY

Part of POSIX.1-2008; GNU coreutils implementation since early versions, originating from 4.3BSD Unix.

SEE ALSO

unexpand(1), fold(1), pr(1), tabs(1)

Copied to clipboard