DDauS
DDauS

Reputation: 105

Replacing varying length text over multiple lines in Vim

Suppose I have a text file open in Vim:

Hello Tom. How's your mom?
HellO Sam. Pass me the Jam.
Hell0 Jim. Do you know Vim?

I can replace the three names with "friend" using the Visual block trick. But what if the three names were of different length?

Hello Kat. You're not fat...
HellO Mike. Where's Pike?
Hell0 James. Hunger games?

Is it possible to replace Cat, Mike and James with "friend"? How about if they were surrounded by the same characters, e.g. html tags?

Hello <Steph>. You seen Jeff?
HellO <Mat>. Here's your hat.
Hell0 <Jenny>. Got a penny?

And if those were doable/easy, is there a way to do it for a single paragraph out of similar paragraphs in a file? Thanks

Upvotes: 0

Views: 268

Answers (2)

SergioAraujo
SergioAraujo

Reputation: 11840

:%norm WcWFriend

:  ...... command mode
%  ...... whole file
norm .... normal mode
W ....... big word (jump to)
cW ...... change word (change it)

Obs: Big words include "-" separated and html tags

Upvotes: 1

Sato Katsura
Sato Katsura

Reputation: 3086

But what if the three names were of different length?

:%s/\m\w\+\ze\./friend/

This replaces the word before the first period on each line.

How about if they were surrounded by the same characters, e.g. html tags?

:%s/\m\w\+\ze>\./friend/

This replaces the word before the first pair >. on each line.

Also:

:%s/\m\<\%(Steph\|Mat\|Jenny\)\>/friend/

This replaces the first occurrence of (either) Steph, Mat, and Jenny on each line.

is there a way to do it for a single paragraph out of similar paragraphs in a file?

Depending on what you mean by "paragraph", you can use :global. Or not. :)

Upvotes: 2

Related Questions