expand(1)


expand, unexpand -- convert tabs and spaces in text files

Synopsis

expand [-t tablist][file ...]

expand [-tabstop] [-tab1,tab2,...,tabn] [file ...]

unexpand [-a|-t tablist] [file ...]

Description

The expand command writes files or the standard input to the standard output with tab characters replaced with one or more space characters needed to pad to the next tab stop. Any backspace characters will be copied to the output and cause the column position count for tab stop calculations to be decremented; the column position count is not decremented below zero.

The unexpand command copies files or standard input to standard output, converting blank characters at the beginning of each line into the maximum number of tab characters followed by the minimum number of space characters needed to fill the same column positions originally filled by the translated blank characters. By default, tabstops are set at every eighth column position. Each backspace character is copied to the output, and causes the column position count for tab calculations to be decremented; the count will never be decremented to a value less than one.

Flags

The following options are supported:

-a
For unexpand only, in addition to translating blank characters at the beginning of each line, translate all sequences of two or more blank characters immediately preceding a tab stop to the maximum number of tab characters followed by the minimum number of space characters needed to fill the same column positions originally filled by the translated blank characters.

-t tablist
Specify the tab stops. The argument tablist must consist of a single positive decimal integer or multiple positive decimal integers, separated by blank characters or commas, in ascending order. If a single number is given, tabs will be set tablist column positions apart instead of the default 8. If multiple numbers are given, the tabs will be set at those specific column positions. Each tab-stop position must be an integer value greater than zero, and the list must be in strictly ascending order. This is taken to mean that, from the start of a line of output, tabbing to position N causes the next character output to be in the (N+1)th column position on that line.

In the event of expand having to process a tab character at a position beyond the last of those specified in a multiple tab-stop list, the tab character is replaced by a single space character in the output. In the obsolescent version, the single number is specified as tabstop with a leading minus; multiple tab stops are specified after a leading minus as tab1,tab2... and so forth.

The unexpand command makes no space-to-tab character conversions for characters that occur at positions beyond the last of those specified in a multiple tab-stop list.

For unexpand, when -t is specified, the presence or absence of the -a option is ignored; conversion will not be limited to the processing of leading blank characters.

Operands

The following operands are supported:

file
The pathname of a text file to be used as input.

Input

Input files must be text files.

Environment variables

The following environment variables affect the execution of expand:

LANG
Provide a default value for the internationalization variables that are unset or null. If LANG is unset or null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL
If set to a non-empty string value, override the values of all the other internationalization variables.

LC_CTYPE
Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments).

LC_MESSAGES
Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

Output

For expand, the standard output is equivalent to the input files with tab characters converted into the appropriate number of space characters.

For unexpand, the standard output is equivalent to the input files with the specified space to tab character conversions.

Exit codes

An exit code of 0 indicates successful completion; an exit code greater than 0 indicates an error.

Diagnostics

The expand command terminates with an error message and non-zero exit status upon encountering difficulties accessing one of the file operands.

Usage

One non-intuitive aspect of unexpand is its restriction to leading spaces when neither -a nor -t are specified. Users who desire to always convert all spaces in a file can easily alias unexpand to use the -a or -t 8 options, as in the following:

alias unexpand="unexpand -a"
alias unexpand="unexpand -t 8"

References

tabs(1).
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 25 April 2004