Vime Regex replacement question

I am trying to replace all several "-" characters in VIM (starting at the beginning of lines) with "="

pe replace "-----" with "====="
or replace "----------" with "==========="

I created this regex:

%s/^-\{2,}/=  ????/g

Does anyone know how I can reproduce the substitution "="? (what I need to put after "=")

+3
source share
3 answers

Try the following:

:%s/^-\{2,}/\=substitute(submatch(0), '-','=','g')/

or

:%s/^-\{2,}/\=repeat('=',len(submatch(0)))/

See more details :help sub-replace-\=.

+5
source

I'm sure there is a better answer, but, in fact, I would do this as two separate operations just for simplicity:

%s/--/==/g
%s/=-/==/g

, ----- ====-. (=-), . , .

+2

Technically, he %s/-/=/gdoes the job, but in the whole file, in each -.

If the lines you want to replace begin with -, I would do it like this:

g/^-/s/-/=/g

Or, if you have a space before the first -:

g/^\s*-/s/-/=/g

The remaining problem occurs in the following lines:

----------- the-composite-word

They turn into:

=========== the=composite=word

There are many ways to solve this. I am not the master to suggest a very general way, but it can work for a dash between words:

g/^-/s/\w\@<!-/g
+2
source

All Articles