max
max

Reputation: 2657

searching for a pattern and placing it within another in vim

I have about 256 lines in a text file that look like /*0*/L"", I want to remove the last , and then put the remaining as a function argument code.append(/*0*/L""); I tried doing it with vim but I don't have much experience in it. how can we place something within something else in vi or vim?

Upvotes: 0

Views: 41

Answers (2)

Kent
Kent

Reputation: 195069

this line would do the substitution on all lines in your buffer, only if the line ending with comma. No matter you had /*0*/L"", or /*123*/L"",

%s/\v(.*),$/code.append(\1)/

if you want to shrink the sub on certain pattern, change the .* part in the above cmd to fit your needs.

Upvotes: 2

Marth
Marth

Reputation: 24812

:%s#\v(/\*0\*/L""),#code.append(\1);#

  • :%s : substitute all lines
  • # : alternative separator
  • \v : use very magic mode (see :h magic)
  • (/\*0\*/L""), : capture the regex, excluding the trailing comma
  • \1 : insert first captured group

Upvotes: 3

Related Questions