tags:

views:

124

answers:

2

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?

+4  A: 

Under Linux and BSD, look up the expand and unexpand command line tools. expand will convert tabs to spaces, and unexpand performs the opposite operation. The simplest usage is:

expand filename

If you are like me, using 4 spaces for tabs, then:

expand -t 4 filename

By default, expand prints to the standard output and leave the original file in tact. To make in-place replacements, you have at least two choices:

$ cp filename backup
$ expand -t 4 filename > tempfile
$ mv tempfile filename

Or, you can invoke expand within vi:

$ vi filename
:%!expand -t 4
Hai Vu
+5  A: 

In Vim:

:retab

or, if you have tabs after spaces:

:retab!
Casey
Don't forget to set expandtab
rampion