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.
Text-Fu · Lesson 10
expand and unexpand
Learn how tab stops control conversion between tabs and spaces with expand and unexpand.
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.
With 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.
Which 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.
Which 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.
Without -a, which spaces does GNU unexpand normally consider for conversion?
Which 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.
Lesson complete
You finished expand and unexpand
You can now convert tabs and spaces while preserving tab-stop alignment.
Expand tabs to the next configured stop.
Set custom tab stops with
-t.Save output to a different file before replacing an input.
Convert leading blanks with
unexpandby default.Use
-awhen blanks throughout each line should be considered.
Keep your learning progress
Create a free account to save this lesson and continue learning on any device.
Create a free account