The Indentation War
In the world of computer programming and system administration, there is a massive, never-ending religious war over how to properly indent lines of code. Half of all programmers prefer to press the “Tab” key to indent their code, because a single invisible Tab character takes up very little data but provides a massive, easily readable block of empty space on the screen.
The other half of programmers violently despise the Tab key. They believe that every single indentation must be created by pressing the “Spacebar” four (or eight) times. They argue that because different text editors render Tab characters at different widths (e.g., some editors display a Tab as 4 spaces wide, others as 8 spaces wide), code formatted with Tabs will look broken and chaotic if you email it to someone else.
If you inherit a massive Python script from a developer who rigidly adhered to the “Spaces” philosophy, and your entire corporate coding standard demands the use of “Tabs,” you have a problem. You cannot manually delete millions of spaces and replace them with Tab characters. To mathematically compress massive blocks of empty spaces back into single, efficient Tab characters, you must use the unexpand command.
Step 1: The Basic Compression
The unexpand command is a structural formatting utility. It scans a text file line by line, hunting for consecutive blocks of empty spaces, and algorithmically replaces them with standard Tab characters.
Assume you have a file named spaced_code.py that is heavily indented with spaces.
unexpand spaced_code.py
The terminal will output the newly compressed text to the screen. By default, unexpand is incredibly conservative. It will only convert spaces that occur at the absolute beginning of a line (the initial indentation). If a programmer typed five spaces between two words in the middle of a sentence, the standard unexpand command will completely ignore them, ensuring the internal structure of the code remains untouched.
Step 2: Forcing Full Conversion
If you are not formatting Python code, but rather formatting a chaotic data table where someone tried to use the Spacebar to manually align massive columns of numbers, you need a much more aggressive conversion.
You must use the -a (all) flag to force unexpand to hunt down every single block of spaces in the entire file, even if they are buried in the middle of a sentence.
unexpand -a messy_table.txt
This command ruthlessly converts every block of two or more spaces into a Tab character, instantly snapping the chaotic columns of data into perfect, rigid alignment.
Step 3: Defining the Tab Width
The most dangerous aspect of the unexpand command is its mathematical assumption about width. By default, standard Linux systems assume that one Tab character is perfectly equivalent to exactly eight spaces.
If the original programmer indented their code using four spaces, the standard unexpand command will fail to compress them, because four spaces do not equal the eight-space requirement. You must manually override the mathematical threshold using the -t (tabs) flag.
unexpand -t 4 spaced_code.py
This command tells Linux: “Do not wait for eight spaces. The moment you detect four consecutive spaces, instantly compress them into a single Tab character.” This is the ultimate weapon for conforming older scripts to modern, four-space coding standards.
Step 4: Permanently Saving the Changes
Like almost all Linux formatting utilities, the unexpand command only alters the text visibly on your monitor; it does not physically overwrite the original file on your hard drive.
To permanently save the newly compressed, Tab-formatted code so you can execute it or send it to your team, you must redirect the output into a brand new file using the standard bracket (>).
unexpand -t 4 spaced_code.py > tabbed_code.py
By mastering the unexpand command, you gain the ability to instantly resolve the spaces-versus-tabs formatting war, drastically reducing the file size of massive scripts and guaranteeing absolute conformity to your corporate coding standards.