Home > Text Processing & Search > expand

expand: Convert Tabs to Spaces

The expand command converts tab characters in an input stream or file to space characters. It is primarily used to standardize the formatting of code or text files, or to improve readability in environments where tab characters are not displayed correctly.

Overview

expand replaces tab characters in text files or standard input with a specified number of space characters. By default, tabs are converted to 8 spaces, and multiple tab stop positions can also be specified.

Key Features

  • Converts tab characters to space characters
  • Allows specification of tab stop positions
  • Provides an option to convert only leading tabs
  • Supports standard input and file input

Key Options

The main options for the expand command control how tabs are converted.

Conversion Settings

Generated command:

Try combining the commands.

Description:

`expand` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Various usage examples of the expand command.

Default Tab Conversion (8 Spaces)

echo -e "Hello\tWorld" | expand

Converts all tab characters in a file to the default of 8 spaces.

Specify Tab Stop Position to 4 Spaces

echo -e "Hello\tWorld" | expand -t 4

Sets the tab stop position to every 4 spaces, converting tabs to 4 spaces.

Convert Only Leading Tabs

echo -e "\tHello\tWorld" | expand -i

Converts only the tabs at the beginning of the line, leaving tabs in the middle unchanged.

Convert Tabs in a File to a New File

expand source.txt > destination.txt

Converts tabs in source.txt to spaces and saves the output to destination.txt.

Specify Multiple Tab Stop Positions

echo -e "Col1\tCol2\tCol3" | expand -t 4,8,12

Specifies tab stop positions at columns 4, 8, and 12. The first tab will be padded with spaces up to column 4, and the second tab up to column 8.

Tips & Notes

Useful tips and points to note when using the expand command.

unexpand Command

  • Description: This command does the opposite of expand, converting spaces back into tabs. You can use both commands together to flexibly manage text file formatting.

Using Pipelines

  • Description: expand accepts standard input, making it useful for processing text that meets certain conditions with other commands like cat or grep, and then converting tabs. (e.g., cat file.txt | expand)

Caution with Direct File Modification

  • Description: expand does not modify the original file; it outputs the result to standard output. To modify the original file, you must use redirection (>). Be careful not to overwrite the original file, or consider creating a backup. (e.g., expand file.txt > file_expanded.txt)

Same category commands