brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: expand -t 2 turns tabs into spaces, for YAML files that refuse tabs

Linux
~/tips/tip-shell-expand-tabs-to-spaces

expand -t 2 compose.yaml > compose.fixed.yaml

Converts tab characters into spaces, so YAML files with tab indentation load again.

YAML has one rule that catches people over and over: no tabs in indentation. The spec says so outright, “tab characters must not be used in indentation”. So a compose.yaml that someone indented with tabs in their editor gets rejected even though it looks fine.

expand from coreutils converts tabs into spaces. With -t you set the tab stops, so two spaces per level for a YAML file looks like this:

expand -t 2 compose.yaml > compose.fixed.yaml

Check the result, then move it over the original. Don’t redirect straight into the same file, because the shell empties it before expand gets to read it.

If you only want to touch the indentation, add -i. Then expand converts the tabs at the start of each line and leaves tabs after the first non-blank character alone, which matters when a value or a comment intentionally contains one:

expand -i -t 2 compose.yaml > compose.fixed.yaml

Its sibling unexpand goes the other way and turns leading spaces back into tabs, for the odd file like a Makefile where tabs are required.

The caveat is how -t works. It sets tab stops, it doesn’t replace every tab with a fixed number of spaces. A tab moves the text to the next stop, so a tab after a three-letter word with -t 4 becomes a single space, while a tab at the start of a line becomes four. For indentation that’s what you want. For tabs in the middle of a line the result can look different from what you expected, which is one more reason I’d use -i on config files.

Further reading: expand(1) manual page, YAML 1.2.2 specification

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link