3

I want to use a regex to replace some strings in my file. I search for: %s/^ [a-z]*/ / what I want to do is to replace every [a-z]* that have 2 whitespaces with the sane [a-z] prepended with 4 whitespaces. Is there any "inplace" replacement or how would I reach that with vim?

With best regards

1
  • Do you mean replace [][]abcd with [][][][]abcd in which [] stands for space? Commented Jun 30, 2011 at 10:02

2 Answers 2

5
:%s/  \([a-z]*\)/    \1/g

should do the job; beware of running this multiple times, though because the result of the replace will match the input pattern :)

Sign up to request clarification or add additional context in comments.

1 Comment

I am very sad this was down voted, that \1 is priceless! thanks for the awesome tip :)
3

I find it more straightforward to use the \ze object to define the end of the match:

:%s/  \ze[a-z]*/    /g

so the [a-z]* is not included in the replace, but just used to match the relevant spaces.

2 Comments

This will also affect lines, prepended with 4 whitespaces.
I like that approach. It'll also be more efficient (I'd be surprised if that would ever lead to noticable speed differences, but hey, I'm a programmer).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.