1

Basically, how can I add log statements like this with Vim?

Initial document (pseudocode):

   case VALUE_1:
       doFunc1();
   case VALUE_2:
       doFunc2();
       doAnotherFunc();
   case VALUE_THREE:
       doFunc3();

What I want to get:

   case VALUE_1:
       print("processing value: VALUE_1");
       doFunc1();
   case VALUE_2:
       print("processing value: VALUE_2");
       doFunc2();
       doAnotherFunc();
   case VALUE_THREE:
       print("processing value: VALUE_THREE");
       doFunc3();
3
  • I'd just type it, possibly copying and pasting the variable names. Your title indicates you already prepared some kind of :substitute command. Please edit and share.
    – Friedrich
    Jan 29 at 15:54
  • What did you try?
    – romainl
    Jan 29 at 15:59
  • @Friedrich @romani well, I know how to insert new line after a search pattern with a global command using g/serach/normal! oMy text but that's the same text on every line, here I need different text, depending on the line I'm inserting after
    – xaxa
    Jan 29 at 16:32

2 Answers 2

2

I would do:

:%s/\vcase (\S*):/\0\r       print("processing value: \1");/

Or to deal better with the various indentation:

:%s/\v^(\s*)case (\S*):/\0\r\1    print("processing value: \2");/

Or as suggested by @Friedrich:

:%s/\vcase (\S*):/\0\rprint("processing value: \1");/

and ask Vim to reindent the code using the = operator.

3
  • 2
    The leading whitespace is likely to be off in the general case. Better omit it and indent using ==.
    – Friedrich
    Jan 29 at 16:09
  • Works like a charm, thank you both
    – xaxa
    Jan 29 at 16:56
  • Thanks for the feedback :-) Jan 29 at 17:40
1

Actually, in your example you only need one print statement to leverage the full variable substitution power of the pseudocode interpreter to do your work

print("processing value: ", foo)
switch (foo)
    case VALUE_1:
        doFunc1();
    ...

This will work unless switch-case is broken, in which case you have a much bigger problem.

Let's assume we want to add a print to each case, anyway. I'd move my cursor on VALUE_1 and then

yawoprint("processing value: <C-R>0");<Esc>

You could turn the line above into a mapping or a macro if you wanted. I don't, so I won't.

Anyway, it will make your code look like this

    case VALUE_1:
        print("processing value: VALUE_1");
        doFunc1();
    case VALUE_2:
        doFunc2();
        doAnotherFunc();
    case VALUE_THREE:
        doFunc3();

In fact, I would not repeat but yank the line I just wrote using yy, search to the next case with /case, put the line and increment the 1 using Ctrl-A.

    case VALUE_1:
        print("processing value: VALUE_1");
        doFunc1();
    case VALUE_2:
        print("processing value: VALUE_2");
        doFunc2();
        doAnotherFunc();
    case VALUE_THREE:
        doFunc3();

For the final print, I can just repeat the last search with n, p again, jump to the 1 with f1 (that's f and 1, not F1) and change it using cwTHREE<Esc>

    case VALUE_1:
        print("processing value: VALUE_1");
        doFunc1();
    case VALUE_2:
        print("processing value: VALUE_2");
        doFunc2();
        doAnotherFunc();
    case VALUE_THREE:
        print("processing value: VALUE_THREE");
        doFunc3();

Done. And we didn't even have to concern ourselves with the arcane knowledge that is regex.

Your Answer

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

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