Hi

Dominic Evans wrote:
> I like to have leading spaces as tabs, but all spaces after any
> non-whitespace character has occurred to be regular spaces.
> 
> Unfortunately :retab! performs the spaces->tab substitution throughout the 
> file.
> 
> In the past I have got around this by using the following replace
> instead (where ^I = tab)
> 
> :%s/^\([^I]*\)    /\1^I/g
> 
> This replaces sequences of 4 spaces immediately at the start of the
> line, or immediately after a number of tabs at the start of the line.
> Due to its nature I have to run this several times to achieve the
> desired result.
> 
> Therefore I have two questions:
> 
> a) could we have a variant of :retab that performs this function - I
> believe it is a fairly common way of having source code
> 
> b) is there a way of making the substitution automatically re-run
> itself until no more possibilities exist?

the following commands replace leading spaces by tabs according to the
current value of the 'tabstop' option

  :%s/^ \+/\=substitute(submatch(0), repeat(' ', &tabstop), "\t", 'g')

  :%s/^ \+/\=substitute(submatch(0), ' \{' . &tabstop . '\}', "\t", 'g')

The first command only works in VIM 7.0 due to the use of the new
repeat() function, but the second one should work in all versions.

Regards,
Jürgen

-- 
Jürgen Krämer                              Softwareentwicklung
HABEL GmbH & Co. KG                        mailto:[EMAIL PROTECTED]
Hinteres Öschle 2                          Tel: +49 / 74 61 / 93 53 - 15
78604 Rietheim-Weilheim                    Fax: +49 / 74 61 / 93 53 - 99

Reply via email to