Blog/Spaces to Tabs

From Forza's ramblings

2021-01-25: Spaces to Tabs[edit | edit source]

Small things can sometimes be really annoying. For example when copy-pasting some code over a remote shell, all tabs would be expanded into normal spaces.

Screenshot of a text with spaces instead of tabs.

While many text editors can convert tabs to spaces, the reverse isn't so common. Luckily there is a simple tool called unexpand from GNU coreutils that does just that!

Normally tab stops are 2, 4 or 8 spaces. And so depending on your source text you would simply call unexpand with the -t flag.

# unexpand -t 4 somefile > somefile.tabs

Adding -a is often useful. It will convert all sequences of two or more blanks just before a tab stop, even if they occur after non-blank characters in a line.

Full manual for the unexpand utility is available from GNU https://www.gnu.org/software/coreutils/manual/html_node/unexpand-invocation.html