I need to repeat a certain operation across several lines of code spread through out the code base. For instance,

x = y;

needs to be replaced with


However each instance needs to be assessed manually, because say this needs to be done only when x and y have been declared as unit16_t. So the functionality I seek is to be able to browse through various files of interest and anytime I come across a line where I feel the above operation needs to run, I should be able to just press a certain sequence of keys that would substitute the given line of code with the desired change. So say if I come across a line somewhere else that looks like below

struct_x.var_x = struct_y.var_y;

I should just be able to replace it with

EQUATE(struct_x.var_x, struct_y.var_y);

with a simple set of keystrokes.

  • You're looking for (untested) :%s#\v\s*(.{-})\s*=\s*(.{-})\s*$#equate(\1, \2)# – Luc Hermitte Apr 8 '17 at 22:43
  • This didn't work - got an error saying NFA regexp Cant have a multi follow a multi. Ended up using a macro instead as documented in the answer link. – JDD Apr 9 '17 at 8:48
  • Answer provided here on Stack Overflow : stackoverflow.com/questions/43304245/… – JDD Apr 9 '17 at 8:50
  • Indeed. As i used \v it should be \= instead of = – Luc Hermitte Apr 9 '17 at 9:25

For the first one:

:s/\(.*\)\s*=\s*\(.*\);/EQUATE(\1, \2);/

Then on subsequent lines, just press & and it will do the same replacement again.

  • This will place everything before the = and everything behind it inside the (), including the ;, for example. Don't use .* here. – Philippos Apr 12 '17 at 10:51
  • Apologies, a typo: I missed the semicolon which I have now edited in. Otherwise, why do you think .* is the wrong thing to use? – Cliff Stanford Apr 13 '17 at 17:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.