>_cmd.script

expand

Convert tabs in input to spaces

Text Processing

By CMD Script Team · 4 min read · Last updated

SYNTAX
expand [OPTIONS] [FILE...]

Options

Command options and flags
FlagDescription
-tSet tab stop width (or a comma-separated list of stops), default is 8
-iConvert only leading tabs (initial whitespace), leaving tabs elsewhere unchanged
--tabsGNU long form of -t for setting tab width

Distribution compatibility

  • Ubuntu
  • Debian
  • Fedora
  • Arch
  • macOS

What it does

expand converts tab characters in its input into the equivalent number of space characters, based on a configurable tab-stop width (default 8), and writes the result to standard output. It's the counterpart to unexpand, which does the reverse. expand is commonly used to normalize whitespace in source files or tabular text so it renders consistently regardless of the viewer's tab-width setting.

Beginner examples

  • expand file.txt — convert all tabs to spaces using 8-space tab stops
  • expand -t 4 file.txt — convert tabs using 4-space tab stops
  • expand -i file.txt — convert only leading (indentation) tabs
  • expand file.txt > file_spaces.txt — save the converted output to a new file
expand -t 4 messy_script.sh > clean_script.sh

Advanced examples

  • Normalize an entire codebase's indentation from tabs to 2-space stops: for f in src/*.py; do expand -t 2 "$f" > "$f.tmp" && mv "$f.tmp" "$f"; done
  • Preview how a tab-indented file will look with a specific tab width before committing to a change: expand -t 4 file.py | less
  • Keep column-aligned data (tabs between columns) intact while normalizing indentation: expand -i report.txt
  • Combine with diff to compare two files ignoring tab-vs-space formatting differences: diff <(expand a.txt) <(expand b.txt)
diff <(expand -t 4 old.py) <(expand -t 4 new.py)

Common mistakes

  • Running expand in place without redirecting to a temp file first — expand file.txt > file.txt truncates the file to empty before expand reads it, just like the same mistake with cat.
  • Assuming the default tab width (8) matches your editor's setting, and getting misaligned output — always check the source file's intended tab width first.
  • Using expand on a file where tabs are meaningful data (e.g. TSV files) instead of formatting whitespace, which destroys the field structure by turning delimiter tabs into spaces.
  • Forgetting -i and expanding tabs used for column alignment mid-line, when only leading indentation should have been touched.

Tips

  • Use expand before diffing two files with inconsistent tab/space indentation to see the real content differences instead of noise from whitespace style.
  • Pair with cat -A first to confirm a file actually uses tabs (^I) before running expand on it.
  • When normalizing a whole project, script the conversion with a loop and write to a temp file, never directly overwrite via redirection.

Best practices

  • Never redirect expand's output back onto its own input file directly; write to a new file or a temp file, then move it into place.
  • Pick a tab width (-t) that matches your project's style guide (commonly 2, 4, or 8) rather than relying on the default.
  • Use -i when a file mixes indentation tabs with meaningful inline tabs (like TSV-style columns) so you don't corrupt the data portion.

Try it yourself

A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.

Real-world use cases

  • Normalizing indentation across a codebase migrated from a tab-based to a space-based style guide.
  • Preparing two versions of a config or script for a clean diff by removing whitespace-style noise: diff <(expand a.conf) <(expand b.conf).
  • Making a tab-indented legacy file readable in tools that render tabs inconsistently, by converting it to spaces before sharing or printing.

Common interview questions

  • What does expand do, and what's its counterpart? It converts tabs to spaces based on a tab-stop width; unexpand does the reverse, converting runs of spaces back to tabs.
  • How do you expand only leading whitespace and leave inline tabs alone? expand -i file — the -i flag limits conversion to the indentation at the start of each line.
  • Why is expand file.txt > file.txt dangerous? The shell truncates file.txt to empty as soon as it opens the redirection target, so expand ends up reading an already-empty file and the original content is lost.

Frequently Asked Questions

Why would I convert tabs to spaces at all?

Tabs render at different widths depending on the editor or terminal's tab-stop setting, which can misalign code or tabular text when viewed in a different tool. Converting to spaces with expand makes the visual layout consistent everywhere.

How do I only expand leading indentation and leave inline tabs alone?

Use expand -i file.txt, which converts only the tabs used for indentation at the start of a line, preserving any tabs that appear later in the line (for example between data columns).

What tab width does expand use by default?

8 characters, matching the traditional terminal default. Use -t to change it, e.g. expand -t 4 file.txt for 4-space tab stops.

Cheat sheet

Download a quick-reference cheat sheet for expand.