From: Keith Thompson on
Noob <root(a)127.0.0.1> writes:
> I'm trying to clean up a C source file with gvim.
> (Might not be the appropriate tool...)
>
> AFAICT, I can delete trailing white space with
>
> :%s,[ \t]\+$,,g
>
> i.e. delete space and tab occurring at least once before newline
>
> However, I don't see how to replace N /leading/ tabs with 2N spaces.
>
> (I don't want to just replace every tab with two spaces because
> there are non-leading tabs which I want to handle differently.)
>
> Matching them would be
>
> /^[\t]\+
>
> but how do I tell the editor to replace N tabs with 2N spaces?

expand -i -t 2

or, if you have GNU coreutils expand and want to be more verbose:

expand --initial --tabs=2

To do it from vi/vim/gvim, type:

:%!expand ...

--
Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
From: Rainer Weikusat on
Keith Thompson <kst-u(a)mib.org> writes:
> Noob <root(a)127.0.0.1> writes:

[...]


>> However, I don't see how to replace N /leading/ tabs with 2N spaces.

[...]

> expand -i -t 2
>
> or, if you have GNU coreutils expand and want to be more verbose:
>
> expand --initial --tabs=2

Without 'GNU coreutils', expand likely won't know anything about -i.
From: Rui Maciel on
Noob wrote:

> Hello,
>
> I'm trying to clean up a C source file with gvim.
> (Might not be the appropriate tool...)
<snip />

As a side note to all the suggestions which were already offered, there is also the
possibility of relying on a 3rd party program to format your code according to a
given coding style.

As you mentioned C source code, I believe GNU indent[1] is your friend. It may
take you a bit of time to get used to it but once you finally get a configuration
that works for you then it's just a matter of feeding it your source code files.
You can even set up your make files to reformat your entire source code tree.


Hope this helps,
Rui Maciel

[1] http://www.gnu.org/software/indent/
From: Stephane CHAZELAS on
2010-01-05, 14:19(+01), Noob:
> Hello,
>
> I'm trying to clean up a C source file with gvim.
> (Might not be the appropriate tool...)
>
> AFAICT, I can delete trailing white space with
>
> :%s,[ \t]\+$,,g
>
> i.e. delete space and tab occurring at least once before newline
>
> However, I don't see how to replace N /leading/ tabs with 2N spaces.
>
> (I don't want to just replace every tab with two spaces because
> there are non-leading tabs which I want to handle differently.)
>
> Matching them would be
>
> /^[\t]\+
>
> but how do I tell the editor to replace N tabs with 2N spaces?
[...]

What about :retab and the 'expandtab' and 'tabstop' options?

Otherwise, to answer the question in the subject:

:s/[^\t]\@<!\t/ /g

It won't work as nicely as :retab if their are combinations of spaces and tabs.


--
St�phane
From: Stephane CHAZELAS on
2010-01-7, 17:19(+00), Stephane CHAZELAS:
[...]
> Otherwise, to answer the question in the subject:
>
> :s/[^\t]\@<!\t/ /g
[...]

Sorry, I meant:

:s/\v([^\t].*)@<!\t/ /g

or

:s/^\t\+/\=substitute(submatch(0),"."," ","g")

--
St�phane