caligula
caligula

Reputation: 192

I can find the expressions but I don't want to replace everything of it (regex in vim)

I tried for some time now but I can't figure it out: I have text that looks like that:

xxx "fy1":
    xxx.xxx = fy;
    xxx;
xxx "tm1":
    xxx.xxx = tm;
    xxx;
...

And I want it to look like that:

xxx "fy1":
    xxx.xxx = fy1;
    xxx;
...

My problem is that I can find all occurences, where I want to put a "1" with

s/[ft][ymdhi];/???/g

but everything I put at the place of the question marks replaces the letters too. The only thing I want to do is put a number after those two letters.

I thought about something with

\w{2}

in the search string too, but that finds everything with two letters before a semicolon, so I think I need the

[ft][ymdhi]

Thanks in advance!

Upvotes: 1

Views: 68

Answers (2)

Kent
Kent

Reputation: 195209

You can make capture-groups so that in replacement reference the matched text. Or you can use vim's \zs to leave some matched text untouched, for example this line does the job:

%s/[ft][ymdhi]\zs;/1;/g

:h \zs and :h \ze for details

If add \ze to this example too, it would be:

%s/[ft][ymdhi]\zs\ze;/1/g

It works too.

Upvotes: 3

René Nyffenegger
René Nyffenegger

Reputation: 40543

You want to use \(...\) along with \1:

:%s/\([ft][ymdhi]\);/\11;/

The escaped paranthes \(...\) sort of store what was matched between the paranthesis. The stored text can later be used again with \1.

In your case the \11 has nothing to to with eleven, it's just a coincidence that the text you remembered with the paranthesis is followed by a 1.

Upvotes: 2

Related Questions