Text-Fu · Lesson 10 of 16

expand and unexpand

0 / 5 checkpoints answered

Full screen ↗

Tabs store movement to a tab stop rather than a fixed number of visible spaces. Their displayed width depends on the current column and the tab-stop settings. The expand and unexpand commands convert between tab characters and spaces while accounting for those positions.

Converting Tabs to Spaces

expand reads input, replaces tabs with the spaces needed to reach the appropriate tab stops, and writes the result to stdout:

$ expand sample.txt

By default, tab stops occur every 8 columns. A tab at column 1 therefore expands differently from a tab at column 6; it is not always replaced by eight spaces.

CheckWith default settings, how does expand replace a tab character?

Choosing Tab Stops

Use -t NUMBER to place tab stops every specified number of columns. For four-column stops:

$ expand -t 4 sample.txt

GNU expand also accepts a comma-separated list of explicit tab positions. Use -i when only tabs before the first nonblank character on each line should be converted.

CheckWhich command converts tabs using tab stops every four columns?

Saving Converted Output Safely

expand does not edit its input file. Redirect stdout to a different pathname when you want to save the converted text:

$ expand sample.txt > result.txt

Do not use expand sample.txt > sample.txt. The shell truncates the destination before expand can read it, so the source data can be lost. After verifying a separately written result, you can deliberately replace the original using an appropriate file-management step.

CheckWhich command saves expanded text without truncating sample.txt before it is read?

Converting Spaces to Tabs

unexpand replaces eligible spaces with tabs while preserving alignment at the selected tab stops. By default, GNU unexpand converts only initial blanks before the first nonblank character on a line:

$ unexpand result.txt

Use -a to consider suitable blanks throughout each line:

$ unexpand -a result.txt

This does not simply replace every run of eight spaces. Conversion depends on column positions and tab stops, just as it does for expand. Use -t 4 or another tab-stop specification when the file follows a different convention.

CheckWithout -a, which spaces does GNU unexpand normally consider for conversion?

CheckWhich option tells GNU unexpand to consider blanks after the first nonblank character too?

Both commands read stdin when no file is named, so they can be used in pipelines. Remember that converting to spaces and back may not reconstruct the original choice of tabs and spaces even when the displayed alignment is unchanged.

Summary

You can now convert tabs and spaces while preserving tab-stop alignment.

  1. Expand tabs to the next configured stop.
  2. Set custom tab stops with -t.
  3. Save output to a different file before replacing an input.
  4. Convert leading blanks with unexpand by default.
  5. Use -a when blanks throughout each line should be considered.