Skip to Content
gnux

unexpandx

Convert spaces to tabs.

Mirrors GNU unexpand. Run it as unexpandx, or as gnux unexpandx.

Synopsis

unexpandx [OPTION]... [FILE]...

GNU reference

Description

unexpandx writes each FILE to standard output with runs of blanks replaced by tabs where they reach a tab stop. With no FILE, or when FILE is -, it reads standard input. By default only the blanks at the start of each line are converted; -a converts every run of two or more blanks that ends at a tab stop, and --first-only goes back to the leading blanks only. Tab stops are every 8 columns unless -t says otherwise, with the same syntax as expandx: -t N, or -t LIST with an optional final /N or +N. -t implies -a, as in GNU. Several -t options add to one list, and the obsolete -N form (unexpandx -4) is a list of its own, in the order given, without implying -a; GNU's comma form -4,8 is not accepted, and the digits of two -N options in a row run together (-4 -8 reads as -48), so write -t 4,8 instead. When both -a and --first-only are given, --first-only wins.

Permissions

Needs read permission on each FILE and search permission on the directories leading to it. Nothing is ever modified.

Use cases

  • Converting space-indented files to tab indentation (Makefiles, Go code).
  • Shrinking files that are padded with spaces.
  • Reversing expandx after editing.
  • Converting the indentation alone, leaving aligned columns inside lines untouched.
  • Normalizing indentation width with -t before a commit.

Flags

  -a, --all            convert all blanks, instead of just initial blanks
      --first-only     convert only leading sequences of blanks (overrides -a)
      --help           display this help and exit
  -t, --tabs N, LIST   have tabs N characters apart instead of 8 (enables -a), or use comma separated list of explicit tab positions
      --version        output version information and exit

Examples

Turn space indentation back into tabs

unexpandx notes.txt

Shrink a space-padded table by using tabs throughout

unexpandx -a table.txt

Convert 4-space indentation and alignment to tabs

unexpandx -t 4 main.go

Retab a Go file for gofmt, leaving alignment inside lines alone

unexpandx -t 4 --first-only main.go

Run an old script that uses the obsolete form

unexpandx -4 main.go

Retab a report laid out in fixed columns

unexpandx -t 10,20,40 report.txt

Edit with spaces, then hand the file back with tabs

expandx -t 4 a.go | unexpandx -t 4 --first-only > b.go

See the tab that replaces 8 leading spaces

printfx '        x\n' | unexpandx

Restore the tabs a Makefile needs after an editor turned them into spaces

unexpandx -t 8 spaces.mk > Makefile

See how many bytes tabs would save in a large file

unexpandx -a big.txt | wcx -c

Retab a file in the portable gnux shell, the same on every OS

gnux shell -c 'unexpandx -t 4 main.go > tabs.go'

Options

Each option has a page of its own, with its usage and examples.

  • -a, --all: Convert all blanks, instead of just initial blanks.
  • --first-only: Convert only leading sequences of blanks (overrides -a).
  • -t, --tabs: Have tabs N characters apart instead of 8 (enables -a), or use comma separated list of explicit tab positions.

Exit status

StatusMeaning
0success: every operand was processed
1failure: an operand could not be processed (diagnosed on standard error), or a command-line mistake

Help in the terminal

unexpandx --help                # options, first examples, exit status
gnux help unexpandx             # this page: the full manual
gnux help unexpandx --OPTION    # one option in depth, with its examples

See also

On this page