$linuxjunkies
>

unexpand(1)

Convert spaces to tabs in text files.

UbuntuDebianFedoraArch

Synopsis

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

Description

The unexpand command replaces leading sequences of spaces with tabs in input lines. By default, it converts spaces at the beginning of lines and between words to tabs, useful for reducing file size or reformatting code to use tab indentation.

If no files are specified, unexpand reads from standard input. Output is written to standard output unless the -i option is used to modify files in place.

Common options

FlagWhat it does
-a, --allConvert all spaces (not just leading) to tabs, using tab stops every 8 characters by default
-t, --tabs=NSet tab stops at N characters apart (default 8); can specify multiple positions with commas
-i, --in-placeModify files in place instead of writing to standard output
--first-onlyOnly convert spaces before the first non-space character on each line
--helpDisplay help message and exit
--versionOutput version information and exit

Examples

Convert leading spaces to tabs in file.txt and display result to stdout

unexpand file.txt

Convert all spaces (not just leading) to tabs throughout the entire file

unexpand -a file.txt

Use tab stops every 4 characters instead of the default 8

unexpand -t 4 file.txt

Modify all .py files in place, converting spaces to tabs

unexpand -i *.py

Pipe file through unexpand and view result with less pager

cat file.txt | unexpand -a | less

Convert only leading spaces before the first non-space character on each line

unexpand --first-only code.c

Related commands