unexpand
Quick Reference
Command Name:
unexpand
Category:
text processing
Platform:
linux
Basic Usage:
Common Use Cases
- 1
Space compression
Convert spaces to tabs in text data
- 2
Text processing
Manipulate text data in pipelines and scripts
- 3
Data cleaning
Clean and sanitize text data
- 4
Scripting
Use in shell scripts to process text data programmatically
Syntax
unexpand [OPTION]... [FILE]...
Options
| Option | Description |
|---|---|
| -a, --all | Convert all sequences of spaces (not just leading spaces) to tabs where possible. |
| --first-only | Convert only leading sequences of spaces on each line (opposite of -a). |
| -t N, --tabs=N | Set tab stops to every N columns instead of the default 8. This determines how many spaces are replaced by each tab. |
| -t LIST, --tabs=LIST | Use a comma-separated list of tab positions (e.g., 2,4,8). Each number specifies a column position for a tab stop. |
| --help | Display help information and exit. |
| --version | Output version information and exit. |
Examples
How to Use These Examples
The examples below show common ways to use the unexpand command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.
Basic Examples:
unexpand file_with_spaces.txt
Converts leading spaces in each line to tabs (using the default 8-space tab stops) and outputs the result to standard output.
unexpand -t 4 file_with_spaces.txt
Converts each group of 4 spaces at the beginning of lines to a tab. This is useful when working with code that uses 4-space indentation but you want to store it with tabs.
unexpand -t 4 file_with_spaces.txt > file_with_tabs.txt
Converts spaces to tabs (with 4-space tab stops) and saves the result to a new file instead of displaying it on screen.
cat file_with_spaces.txt | unexpand -t 2
Reads input from a pipe instead of a file, converting groups of 2 spaces to tabs. This demonstrates how unexpand can work within command pipelines.
Advanced Examples:
unexpand -a file_with_spaces.txt
Converts all spaces, not just leading ones, to tabs when possible. This maximizes tab usage throughout the file for potentially greater space savings.
unexpand -t 4 --first-only file_with_spaces.txt
Converts only the spaces at the beginning of each line to tabs, using 4 spaces per tab. This is useful for handling indentation while leaving inline spacing intact.