gatoatigrado
gatoatigrado

Reputation: 16850

vim -- how to do multiple search-and-replace operations on a visual block?

Suppose I have some code,

struct NodeVector {
    vector<bool_node*> *vec;
};

I want to replace two things, like this,

:'<,'>s/NodeVector/MyClass/g | s/bool_node/MyEltClass/g

but, it only runs the first search, and then says "pattern not found: bool_node". How can I achieve this result? (plugin answers are okay).

struct MyClass {
    vector<MyEltClass*> *vec;
};

Upvotes: 4

Views: 291

Answers (3)

sehe
sehe

Reputation: 393104

In default setups you can shorten it:

:*s/NodeVector/MyClass/g | *s/bool_node/MyEltClass/g

This is because, normally 1, :* is a synonym for :'<,'>


1 unless * is in cpoptions (vi compatibility options), which it isn't by default

Upvotes: 1

kev
kev

Reputation: 161694

vim treats |(bar) differently after :global command, so you can do this:

:'<,'>g/^/s/NodeVector/MyClass/g | s/bool_node/MyEltClass/g

Upvotes: 1

Asumu Takikawa
Asumu Takikawa

Reputation: 8523

The issue here is that both of the search & replace commands need a range. For example, these should work fine:

:'<,'>s/NodeVector/MyClass/g | '<,'>s/bool_node/MyEltClass/g

or

:%s/NodeVector/MyClass/g | %s/bool_node/MyEltClass/g

Upvotes: 4

Related Questions