LinuxCommandLibrary

unexpand

Convert spaces to tabs in a file

TLDR

Convert blanks in each file to tabs, writing to stdout

$ unexpand [path/to/file]
copy

Convert blanks to tabs, reading from stdout
$ unexpand
copy

Convert all blanks, instead of just initial blanks
$ unexpand [[-a|--all]] [path/to/file]
copy

Convert only leading sequences of blanks (overrides -a)
$ unexpand --first-only [path/to/file]
copy

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

SYNOPSIS

unexpand [OPTION]... [FILE]...

PARAMETERS

-a, --all
    Convert all suitable sequences of spaces (not just leading ones) into tabs.

-t N, --tabs=N
    Set tab stops at every N columns. For example, -t 4 sets tab stops at columns 4, 8, 12, etc.

-t LIST, --tabs=LIST
    Set tab stops at specific columns defined by a comma-separated LIST of positive integers in ascending order. For example, -t 5,10,15 sets tab stops at columns 5, 10, and 15.

--help
    Display a help message and exit.

--version
    Display version information and exit.

DESCRIPTION

unexpand is a command-line utility that converts sequences of spaces in files or standard input into tab characters. It serves as the inverse operation to the expand command. By default, unexpand only converts leading spaces into tabs, optimizing for common indentation patterns. However, with the --all option, it can convert all suitable sequences of spaces throughout the line. Users can define custom tab stop positions using the --tabs option, either as a fixed interval or a specific list of column numbers.

This tool is useful for reducing file size, improving readability for text editors that handle tabs efficiently, and restoring files that might have been processed by expand previously. It helps in maintaining consistent formatting by using tabs where appropriate, which can be more efficient than using only spaces for indentation.

CAVEATS

By default, unexpand is conservative, only converting leading spaces to avoid altering visual alignment within text that relies on spaces. When using the --all option, be cautious, as it might inadvertently change the visual layout of non-indented text if not handled carefully, especially when viewing the file with different tab stop settings in various editors.

For precise reversal of an expand operation, ensure that the --tabs settings for unexpand exactly match those used during the original expand conversion.

DEFAULT BEHAVIOR

When no options are specified, unexpand acts conservatively. It only converts sequences of leading spaces (those at the beginning of a line or immediately following another tab character) into tabs. This approach helps optimize indentation while preserving the visual integrity of other parts of the text that may rely on specific spacing for alignment.

TAB STOP LOGIC

When converting spaces to tabs, unexpand determines the number of spaces to replace based on the current column position and the defined tab stops. It intelligently inserts the minimum number of tab characters and remaining spaces to reach the next tab stop or to accurately represent the original spacing. This process ensures that the visual alignment of the text is maintained as much as possible.

HISTORY

unexpand is an integral part of the GNU Core Utilities (coreutils) package, a fundamental collection of command-line tools found on Linux and other Unix-like operating systems. Its development, like that of expand, has been driven by the need for efficient and flexible handling of whitespace in text files, evolving alongside standard Unix utilities to provide robust text processing capabilities.

SEE ALSO

expand(1), pr(1), tabs(1)

Copied to clipboard