开发者

vim search-replace question

开发者 https://www.devze.com 2022-12-14 12:16 出处:网络
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

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?


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.


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__


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__);  


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号