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]...
expand [OPTION]... --files-from=F

PARAMETERS

-t N, --tabs=N
    Set tab stops at every N columns. Each tab character is replaced by enough spaces to reach the next multiple of N column position.

-t N1,N2,..., --tabs=N1,N2,...
    Set tab stops at specific column positions: N1, N2, etc. Subsequent tabs will expand to the next specified stop. If a tab goes beyond the last specified stop, it expands to a uniform width (defaulting to 8 spaces) relative to that last stop.

-i, --initial
    Only convert initial tabs (tabs at the beginning of a line) and not tabs appearing later on the line.

--help
    Display a help message and exit.

--version
    Output version information and exit.

DESCRIPTION

The expand command is a standard Linux utility designed to convert tab characters into an equivalent number of space characters within files or from standard input. This is particularly useful for ensuring consistent indentation in source code, configuration files, or plain text documents, as different text editors or display environments might interpret tab characters differently. By default, expand replaces each tab with 8 space characters. However, users can specify custom tab stop settings, either by defining a uniform tab width (e.g., 4 spaces per tab) or by providing a list of absolute column positions where tab stops should occur. When a tab character is encountered, expand inserts enough spaces to reach the next defined tab stop. It processes the input line by line, printing the modified content to standard output, making it an invaluable tool for formatting consistency.

CAVEATS

When specifying absolute tab stops (e.g., -t 5,10,15), the numbers must be in strictly increasing order. If they are not, expand may report an error or exhibit unexpected behavior. The command works on byte offsets, meaning it correctly handles multi-byte characters in UTF-8 locales if the terminal also interprets them correctly, but its column calculations are based on monospace characters.

DEFAULT TAB BEHAVIOR

If no tab stops are specified using -t or --tabs, expand defaults to setting tab stops every 8 columns. This means each tab character encountered will be replaced by enough spaces to reach the next column position that is a multiple of 8.

INPUT AND OUTPUT FLOW

By default, if no FILE is specified, expand reads its input from standard input (stdin) and writes the processed, expanded content to standard output (stdout). This makes it highly versatile for use within shell pipelines, allowing it to easily chain with other commands (e.g., cat file.txt | expand -t 4 | less).

HISTORY

The expand command is a foundational Unix utility, part of the core text processing tools that have been available since early versions of the Unix operating system. It was standardized by POSIX (Portable Operating System Interface) to ensure consistent behavior across different Unix-like systems. Its design predates many modern text editors and IDEs that incorporate built-in tab-to-space conversion, making it a crucial tool for batch processing or preparing files for environments without such advanced features. Its continued inclusion in GNU Core Utilities underscores its enduring utility for basic text manipulation tasks.

SEE ALSO

unexpand(1), pr(1), sed(1), awk(1), tr(1)

Copied to clipboard