William Chung
William Chung

Reputation: 35

vi vim delete a word in a special block

I want to delete "go"s in the comment only, in vi or vim. Would you please let me know how?

/*
the following gos should be deleted: in the comment
go
*/

the following go should not be deleted
go


/*
the following go should be deleted: in the comment
go

and some more words
go
*/

the following go should not be deleted
go

As a result of delete should be as follows:

/*
the following gos should be deleted: in the comment
*/

the following go should not be deleted
go


/*
the following go should be deleted: in the comment

and some more words
*/

the following go should not be deleted
go

Thanks.

Upvotes: 3

Views: 88

Answers (2)

mMontu
mMontu

Reputation: 9273

You could try this:

g/\/\*/.,/\*\//s/\<go\>//g
  • g/<pattern>/ - matches lines containing a given pattern, in this case a start of comment
  • .,/<patter>/ - execute the following ex command from the current line to the next line that matches a pattern, which in this example is the end of comment
  • s/<pattern>//g - replaces all the occurrences of the pattern on each line with an empty string

Upvotes: 2

Kent
Kent

Reputation: 195269

this line works for your example:

%s#/\*\zs\_.\{-}\ze\*/#\=substitute(submatch(0),'go','','g')#

some items you may want to take a look in vim's great help doc:

:h \zs
:h \ze
:h \_.
:h /star
:h :s\=
:h substitute(

Upvotes: 3

Related Questions