expand

expand converts tab characters into spaces, so that text lines up the same way no matter what tab width something is viewed with.

expand [options] [file...]
$ expand source.txt > source-spaces.txt

A tab is not a fixed number of spaces — it jumps to the next tab stop, and where those stops are is a display setting. expand removes the ambiguity: it replaces each tab with exactly the number of spaces needed to reach the same column, baking the layout in. With no file, it reads standard input.

unexpand is the reverse operation.

Options #

OptionEffect
-t, --tabs=NSet tab stops every N columns instead of the default 8.
-t, --tabs=LISTGiven a comma-separated list, place tab stops at those explicit column positions.
-i, --initialConvert only the tabs that come before the first non-blank character on each line — leave tabs within the text alone. Useful for re-indenting code without disturbing aligned tabs inside it.

Exit status #

CodeMeaning
0Success.
1A file could not be read, or a tab specification was invalid.

Edit this page