expand
Convert tabs in input to spaces
By CMD Script Team · 4 min read · Last updated
expand [OPTIONS] [FILE...]Options
| Flag | Description |
|---|---|
-t | Set tab stop width (or a comma-separated list of stops), default is 8 |
-i | Convert only leading tabs (initial whitespace), leaving tabs elsewhere unchanged |
--tabs | GNU 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 stopsexpand -t 4 file.txt— convert tabs using 4-space tab stopsexpand -i file.txt— convert only leading (indentation) tabsexpand 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
diffto 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
expandin place without redirecting to a temp file first —expand file.txt > file.txttruncates the file to empty beforeexpandreads it, just like the same mistake withcat. - 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
expandon 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
-iand expanding tabs used for column alignment mid-line, when only leading indentation should have been touched.
Tips
- Use
expandbefore diffing two files with inconsistent tab/space indentation to see the real content differences instead of noise from whitespace style. - Pair with
cat -Afirst to confirm a file actually uses tabs (^I) before runningexpandon 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
-iwhen 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
diffby 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
expanddo, and what's its counterpart? It converts tabs to spaces based on a tab-stop width;unexpanddoes 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-iflag limits conversion to the indentation at the start of each line. - Why is
expand file.txt > file.txtdangerous? The shell truncatesfile.txtto empty as soon as it opens the redirection target, soexpandends 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.