How can I replace intraline tabs with spaces, maintaining alignment?

regex, sed, vim

Solution

In Vim:

:retab

or, if you have tabs after spaces:

:retab!

Problem

I like to use spaces for indentation rather than tabs; replacing tabs at the beginning of a line is easy in `sed` or `vim`: ``` s/^I/ /g ``` But if there are tabs within a line (pretend the spaces are the width of the tab char): ``` 'foo'^I ^I => 'bar', 'bazzle'^I => 'qux', ``` Each tab doesn't correspond to a set number of spaces to maintain the alignment. Anyone have a sly idea of how to replaces those tab characters with spaces while keeping the correct alignment?

Original source