Vim - how to perform multiple search and replace operations on a visual block?

Suppose I have a code,

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

I want to replace two things, for example:

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

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

 struct MyClass { vector<MyEltClass*> *vec; }; 
+4
source share
3 answers

The problem is that both search and replace commands need a range. For example, they should work fine:

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

or

 :%s/NodeVector/MyClass/g | %s/bool_node/MyEltClass/g 
+4
source

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

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

In the default settings, you can shorten it:

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

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


1 if * not in cpoptions (vi compatibility options), which is not specified by default

+1
source

Source: https://habr.com/ru/post/1398521/


All Articles