How to Use the Linux expand Command to Convert Tabs to Spaces

A long-standing debate among programmers is whether to use “Tabs” or “Spaces” for indenting lines of code. While modern text editors handle both relatively well, transferring a file created with Tab characters to a system or script that strictly expects Spaces can cause formatting errors, broken code logic (especially in languages like Python), and unreadable terminal output. If you are a Linux user dealing with a file heavily indented with tabs, you do not need to open it in a graphical text editor to fix the formatting. The Linux expand command is a simple, dedicated utility designed to read a file and convert every single Tab character into an equivalent number of space characters.

Basic Tab to Space Conversion

By default, the expand command assumes that a single Tab character is equivalent to 8 standard spaces. If this matches your desired formatting, the conversion process is incredibly straightforward.

  1. Open your terminal application.
  2. Locate the file containing the tabs (e.g., script.py).
  3. Type the command followed by the filename: expand script.py
  4. Press Enter.

The command will instantly read the file, perform the conversion in memory, and print the resulting text (with spaces instead of tabs) directly to your terminal screen (standard output). The original script.py file remains completely untouched and unchanged on your hard drive.

Customizing the Tab Stop Size

An 8-space indentation is generally considered far too wide for modern programming. Most style guides recommend either 2 or 4 spaces per indentation level. To force the expand command to convert each Tab into a specific number of spaces, you must use the -t (tabs) flag.

  • Convert to 4 spaces: To replace every tab with exactly 4 spaces, run the following command: expand -t 4 script.py
  • Convert to 2 spaces: To replace every tab with exactly 2 spaces, run: expand -t 2 script.py

Converting Only Leading Tabs (Initial Indentation)

Sometimes, a file uses tabs for indentation at the start of a line, but also uses tabs inside the line to align columns of data (like a CSV file). If you run the standard expand command, it will convert every single tab it finds, potentially destroying the column alignment within the data.

To tell the command to only convert the initial indentation tabs at the very beginning of a line and ignore any tabs found later in the text, you must use the -i (initial) flag.

expand -i -t 4 data_script.py

Saving the Converted Output

Because expand only prints to the screen, you must use bash redirection (>) to permanently save the space-formatted text into a new file.

expand -t 4 script.py > script_fixed.py

This command creates a brand new file called script_fixed.py containing the perfectly spaced code, leaving your original tabbed file intact as a backup.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.