0

I'm refactoring the logger code in my software.

The old log message ends with either ); or just ; at the end of a line and I want to remove those characters and add a new line with some additional data. Here are a few examples showing the endings:

SNAP_LOG_INFO("Some message");
SNAP_LOG_INFO("Some message ")(variable);
SNAP_LOG_INFO("Some message ")(func());
SNAP_LOG_INFO() << "Some message";

// Here is a special case, but I'm not trying to solve it because it's really rare
SNAP_LOG_INFO() << func();

I built my macro like this:

qs$xxi\n<< SNAP_LOG_SEND;\^[q

(where \n is the Enter key and \^[ is the escape key)

That works great when the line ends with ");" but it deletes one too many characters when the old version ends with just ";".

How can I update my macro to make it work in both cases?

4
  • 1
    %s/)\?;/^M<< SNAP_LOG_SEND where ^M is a CTRL-V CTRL-M
    – Maxim Kim
    Feb 14, 2022 at 19:21
  • And this also should cover your special case: %s/\((\@<!)\)\?;/^M<< SNAP_LOG_SEND
    – Maxim Kim
    Feb 14, 2022 at 19:26
  • When you write %s/... do you mean : rather than %? Feb 14, 2022 at 20:13
  • 1
    :%s/... to target all lines
    – Maxim Kim
    Feb 15, 2022 at 8:06

2 Answers 2

2

Another approach is to use :substitute, having:

SNAP_LOG_INFO("Some message");
SNAP_LOG_INFO("Some message ")(variable);
SNAP_LOG_INFO("Some message ")(func());
SNAP_LOG_INFO() << "Some message";

// Here is a special case, but I'm not trying to solve it because it's really rare
SNAP_LOG_INFO() << func();

You can use :%s/\((\@<!)\)\?;/^M<< SNAP_LOG_SEND to convert it to (Note, ^M is a literal CTRL-V CTRL-M):

SNAP_LOG_INFO("Some message"
<< SNAP_LOG_SEND
SNAP_LOG_INFO("Some message ")(variable
<< SNAP_LOG_SEND
SNAP_LOG_INFO("Some message ")(func()
<< SNAP_LOG_SEND
SNAP_LOG_INFO() << "Some message"
<< SNAP_LOG_SEND

// Here is a special case, but I'm not trying to solve it because it's really rare
SNAP_LOG_INFO() << func()
<< SNAP_LOG_SEND

If that is what you want.

2
  • Okay, note that I also want to verify the results, so substituting in the entire file is problematic. I want to make the change to one log message at a time. Otherwise, the (\@<!) is a nice trick. I suppose it comes from perl... Feb 15, 2022 at 23:02
  • use :%s/.../.../gc to make it "one change to one log message at a time"
    – Maxim Kim
    Feb 16, 2022 at 6:19
0

The problem is to find where to go to the end of the line. For that we can actually use a search, but in a macro it replaces the current search. So you have to restore your current search as well if you use such.

The final macro looks like this:

qs/)\?;$\nC\n<< SNAP_LOG_SEND;\^[k/SNAP_LOG\nq
^ ^  ^    ^                   ^  ^^          ^
| |  |    |                   |  ||          +-- stop macro creation
| |  |    |                   |  |+-- restore my search
| |  |    |                   |  +-- go up one line
| |  |    |                   +-- ESCape to stop editing
| |  |    +-- delete end of line & start editing
| |  +-- make the parenthesis option in this search
| +-- search a search inside the macro
+-- start macro 's'

I use the k and do a search on my text so I can stay on the new line I just created. That way I can see if it worked. Otherwise it would go to my next instance which could be many pages below hiding the changes that just happened.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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