tags:

views:

136

answers:

4

Hi, I have a few trace lines in my file of the form

M_TRACE(EV_TRACE_LEVEL_DEBUG, "some trace");

I want to convert those into

M_TRACE(EV_TRACE_LEVEL_DEBUG, "%s: some trace", __FUNCTION__);

However I have already a few traces which display the function name also.

To make the conversion i am using the following command

:%g/M_TRACE/s/"\(.*\)"/"%s: \1", __FUNCTION__/c

which unfortunately includes a redundant FUNCTION in some places.

Is there any better way to do this?

A: 

You could try:

   :%g/M_TRACE.*\");/s/"\(.*\)"/"%s: \1", __FUNCTION__/c

to exclude lines with an arg after the string.

2nd try: Use Vim's zero-width negative look-behind pattern:

%g/M_TRACE.*\(__FUNCTION__\)\@<!);/s/"\(.*\)"/"%s: \1", __FUNCTION__/

That's M_TRACE followed by a ); thats NOT preceded by __FUNCTION__

Nick Dixon
Unfortunately I had many macros with arguments also. Am sorry not to have mentioned before.
arun kumar
A: 

I would do it in two steps: first replace the part inside the quotes, then replace ") with ", __FUNCTION__)

However, if you just want to do it with just one command:

:%g/M_TRACE/s/"\(.*\)".*/"%s: \1", __FUNCTION__);
kemp
My substitution will did both. It is just that I was not able to select only those lines without __FUNCITON__. Instead of doing it in two steps as u have suggested I used the confirm flag(/c).I want to know if there is an even better way to do that
arun kumar
Well doing two substitutions removes the need for the manual confirmation, sounds like a better way to me.
kemp
A: 

If you have a few (dozen) you can just do an interactive search/replace using interactive mode:

:%g/M_TRACE/s/"\(.*\)"/"%s: \1", __FUNCTION__/igc

and just step through them. Answering y/n as you go.

zen
+1  A: 

You can do this in a single command:

:%g/M_TRACE/s/"\(.*\)"\(, __FUNCTION__\)\?/"%s: \1", __FUNCTION__/

It will replace the ", __FUNCTION__" if its already present or it simply append it if its not present.

RahulJ