
unexpand Command in Linux
The unexpand command in Linux is a tool that changes spaces into tab characters in text files. If files use spaces for alignment or indentation, this tool can switch them back to tabs. This change makes text file structures simpler and may improve readability. It can also reduce the file size in some cases. This is particularly useful for anyone looking to convert files formatted with spaces back to a tab-based format, ensuring more streamlined and organized files.
Table of Contents
Here is a comprehensive guide to the options available with the unexpand command −
- What is unexpand Command in Linux?
- Syntax of unexpand Command
- unexpand Command Options
- Examples of unexpand Command in Linux
What is unexpand Command in Linux?
The unexpand command is a tool used to change spaces in a file into tab characters. By default, it focuses on spaces at the beginning of lines, turning them into tabs. However, if you use extra options, you can set it to change all spaces in a file or choose specific tab positions to make these changes.
Many people use the unexpand command along with the expand command. The expand command does the reverse by converting tabs back into spaces. Using these two commands together allows you to effectively control and manage the formatting of text files, ensuring they look consistent and organized for whatever specific needs you might have.
Syntax of unexpand Command
The basic syntax of the unexpand command is as follows −
unexpand [OPTION]... [FILE]...
Where,
- [OPTION] − These are settings that adjust how the command operates. They can change the way the command processes the information.
- [FILE] − This is where you list the file or files you want to work on. If you don't mention any files, unexpand will automatically read input from the keyboard or from another source connected to the command.
unexpand Command Options
The unexpand command comes with a range of useful options that allow users to control how spaces are converted to tabs in text files.
Option | Description |
---|---|
-a, --all | Converts all spaces in the file into tabs, not just the leading spaces. |
--first-only | Ensures that only leading spaces are converted into tabs, even if the -a option is used. |
-t, --tabs=N | Sets tab stops every N characters instead of the default of 8. This option enables the -a flag automatically. |
-t, --tabs=LIST | Uses a comma-separated list of explicit tab positions for conversion. |
--help | Displays a help message with information about the command and its options. |
--version | Outputs the version of the unexpand command installed on the system. |
Examples of unexpand Command in Linux
Let's take a closer look at how the unexpand command can be used in real-world scenarios to manage text formatting efficiently.
- Converting Only Leading Spaces into Tabs
- Converting All Spaces into Tabs
- Defining Custom Tab Intervals
- Using Explicit Tab Positions
- Restricting Conversion to Leading Spaces Only
Converting Only Leading Spaces into Tabs
This example explains how to turn spaces at the start of lines into tab characters without changing other spaces.
unexpand file.txt
The command typically only targets spaces at the start of each line in a file named "file.txt." It switches these beginning spaces to tabs and displays the updated content on your screen. This way, you can see how the leading spaces have been turned into tabs, while all other spaces in the text remain unchanged.

To save the changes to a new file, redirect the output −
unexpand file.txt > formatted_file.txt
Converting All Spaces into Tabs
When spaces are consistently used for alignment, you can use a command to replace all those spaces with tabs.
unexpand -a file.txt
When you use the -a option with this command, it scans the entire file. It converts every single space into a tab, regardless of its position in the line. This ensures the file remains neatly organized, maintaining a uniform tab-based layout throughout.

Defining Custom Tab Intervals
If you want to set tab stops at specific intervals, you can set them every 4 characters. Here's how to do it −
unexpand -t 4 file.txt
What this does is replace spaces in the file named file.txt with tabs. Instead of using the normal default of 8 spaces, it switches spaces with tabs every 4 spaces. This is really useful for files where exact alignment matters or when you're working with tab sizes that aren't standard.

Using Explicit Tab Positions
If you want to decide exactly where your tabs should be in a text file, you can use a command to make it happen. Here’s an example that sets tab stops at specific points.
unexpand -t 4,8,12 file.txt
What this does is replace spaces in the file.txt with tabs at the positions you choose: positions 4, 8, and 12 in each line. This allows you to have precise control over the layout of your file, making sure tabs are placed exactly where they are needed for your formatting needs.

Restricting Conversion to Leading Spaces Only
This example shows how to ensure only spaces at the start of each line are converted, even when the -a option is active.
unexpand --first-only file.txt
By adding the --first-only option, this command limits the conversion to leading spaces while leaving all other spaces in the file unchanged. This is helpful for files where internal alignment must be preserved.

Processing Input Directly from the Terminal
When working with dynamically generated input, you can use unexpand to process text provided via standard input (STDIN).
echo " ello, World!" | unexpand
The input string " Hello, World!" contains leading spaces, which are converted into a single tab character when passed through the unexpand command. This feature is ideal for quickly testing or processing small snippets of text.

Conclusion
The unexpand command is a helpful tool in Linux for converting spaces into tabs, making text formatting both flexible and accurate. It's easy to use and offers different options to meet your needs. You can change spaces at the start of lines, convert every space in the document, or set specific points for tabs.
The unexpand command is useful in many situations, such as handling configuration files or making scripts better for both storage and readability. Learning to use unexpand effectively will make text processing on Linux systems simpler and more efficient.