detab -- convert tabs to spaces two different ways


detab.pl is a perl program which detabs a text file using two different methods for converting tabs to spaces: 1) a column-based mode which attempts to insert spaces to a "virtual" tab location or 2) a literal number of spaces per tab character. It is especially helpful when receiving text files from collaborators who use different editors or tab settings. It is also good when you get a file with mixed tabs and spaces that cause inconsistent indentation. It's ideal for prepping source code, data files, or documentation for printing, diffing, or formatting clarity.

Limitations

    Usage:  detab.pl [-h][-l #][-m mode] [filename]

    -h       Show this usage message
    -l#      Each tab is worth up to # spaces.  8 spaces per tab is the
             default.
    -m mode  Where mode is one of: 'column' or 'literal', indicating the
             tab replacement mode, either spacing to the actual column
             location indicated by the tab (may be less than -l spaces) or
             a literal replacement of each tab by -l spaces without regard to
             to which column.  column mode is the default.

    If no filename is given, the program will read from stdin
Note: when a program reads input from stdin, it will not print the help screen unless you use the -h option.

This utility is part of my collection of text-processing tools.

Example

For example, if you have text like
"Test[tab]some stuff"
then with 8 chars per tab, in column mode, you'll get
"Test^^^some stuff"
             ^
    column 8

...but in literal mode, you'll get
"Test^^^^^^^^some stuff"
        8 spaces


Back to Kent's Perl Page

Last-modified: Tue Mar 17 10:25:20 EDT 2026