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
expandxafter editing. - Converting the indentation alone, leaving aligned columns inside lines untouched.
- Normalizing indentation width with
-tbefore 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 exitExamples
Turn space indentation back into tabs
unexpandx notes.txtShrink a space-padded table by using tabs throughout
unexpandx -a table.txtConvert 4-space indentation and alignment to tabs
unexpandx -t 4 main.goRetab a Go file for gofmt, leaving alignment inside lines alone
unexpandx -t 4 --first-only main.goRun an old script that uses the obsolete form
unexpandx -4 main.goRetab a report laid out in fixed columns
unexpandx -t 10,20,40 report.txtEdit with spaces, then hand the file back with tabs
expandx -t 4 a.go | unexpandx -t 4 --first-only > b.goSee the tab that replaces 8 leading spaces
printfx ' x\n' | unexpandxRestore the tabs a Makefile needs after an editor turned them into spaces
unexpandx -t 8 spaces.mk > MakefileSee how many bytes tabs would save in a large file
unexpandx -a big.txt | wcx -cRetab 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
| Status | Meaning |
|---|---|
| 0 | success: every operand was processed |
| 1 | failure: 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