Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim regex find and replace

Tags:

regex

vim

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

like image 382
Martin Avatar asked Aug 05 '26 18:08

Martin


2 Answers

:%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 :)

like image 89
sehe Avatar answered Aug 07 '26 06:08

sehe


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.

like image 24
Prince Goulash Avatar answered Aug 07 '26 08:08

Prince Goulash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!