Question

i have a piece of code like this

int a=8;
cout<<"a is :"<<a<<endl;     // show what is the value of a
a=a*a;
string b="ice";              // b is ice
const char* c=b.c_str();     

this code repeats at many places in exact same way. I want to find all the occurrence of this piece of code. I was thinking I could copy the code and put it into one of the registers and use the content of register to perform the matching. The text can have any of the special characters and while matching, it needs to be treated as ordinary text. Is it possible to match using the register content? in what other way I can accomplish the same?

Was it helpful?

Solution

Plugin

This becomes even easier by selecting the lines, and then searching for the visual selection, and you don't clobber a register through this. There are several plugins that extend the built-in normal mode * command to visual mode, also my SearchHighlighting plugin. (The plugin page has links to alternative plugins.)

Manually

If you want to do this manually, through a register, use a very nomagic (\V) pattern, and escape the special characters. To insert the (escaped) register contents into the command-line, insert register <C-R> with the expression register is used:

/\V<C-R>=substitute(escape(@@, '/\'), '\n', '\\n', 'g')<CR><CR>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top