← retour aux snippets

expand/unexpand: gérer les tabulations

Convertir tabs en espaces et inversement pour normaliser des fichiers.

objectif

Nettoyer des alignements basés sur tabulations pour un rendu constant.

code minimal

# tabs -> espaces (4 colonnes)
expand -t 4 input.txt > spaces.txt

utilisation

# espaces -> tabs (économie d'octets)
unexpand -t 4 -a spaces.txt > tabs.txt

variante(s) utile(s)

# normaliser un projet entier
find . -type f -name '*.ts' -print0 | xargs -0 -I{} sh -c 'expand -t 2 "{}" > "{}.tmp" && mv -f "{}.tmp" "{}"'

notes

  • -a convertit toutes les séquences d’espaces en tabulations.
  • attention aux contextes où les tabs sont proscrites.