0

How can I repeat the execution of a macro until a character is encountered?

For example (just an example, it's not the original problem I want you to solve) I want to convert a CAPITAL_CAMEL_CASE to Sentence case in the following line using the macro as follows: ^7wviwu~, now since I have Some_string_constant, I want to replace _ with a (space), for that _ have written another macro f_r<space>.

private static final String SOME_STRING_CONSTANT = "SOME_STRING_CONSTANT";

Now different lines have different number of _ and I need to carefully manually count and repeat the second macro. How can I repeat second macro until I encounter " as I am trying to execute macro only on the string enclosed in "?

Note: I am using a vim plugin in IDE which does not support a replacement for a selection.

2
  • tpope's abolish plugin has support for changing from cases
    – D. Ben Knoble
    Jul 26 at 16:05
  • Depends on whether IDE supports vim plugins, which is probably not the case
    – Maxim Kim
    Jul 26 at 16:27
2

In vim (not sure about other IDE plugins)

  • When there is a "fail" in a macro it stops execution.
  • When f<char> can't find next char it "fails".

So you can

  1. Create a macro @r that does only f_r<space>.
  2. Use it in other macro @q that executes @r with a large count.
  3. Or use it in :normal command if your IDE supports it.

Example:

enter image description here

PS, simpler sequences

Macro:

qqvi"u~h100@rq

Normal command:

:'<,'>normal! vi"u~h100@r

where @r is qrf_r q

2
  • 1
    Another option would be a recursive macro that repeats the f_r step: qrqqrf_r @rq, I think
    – D. Ben Knoble
    Jul 26 at 16:06
  • Yes indeed. I don't like recursive macros thus didn't even think about them ;)
    – Maxim Kim
    Jul 26 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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