0

There are occasions where I use macros to re-format multiple lines, where the lines can have a different format. For these tasks, I use appropriate macros, but due to the different formats this can fail. In these cases I might use multiple macros that I then use accordingly to the current line, but I was wondering if there is a better way.

As an example: I was re-formatting the following lines, where I wanted every entry of the lua tables to be on their own line:

{ foo = 1, bar = 2 },
{ foo = 42, bar = -4 },
{ foo = 1, bar = 9, baz = "boo" },

In this case I'd use a macro that inserts new lines after commas and also puts the closing } on its own line (of course some of this can be fixed by fitting :s commands, but this is a simple example, these can get more complex.

Is there a good way to tackle these cases? I'd also be happy to use a macro that inserts one line but repeats itself until there are no more matches on this line. However, I also would not know how to do that.

1
  • 1
    This sounds like a job for a formatter.
    – romainl
    Nov 16, 2023 at 7:09

1 Answer 1

1

I would have the following macro:

/\v(,|.})^Ma^M^[

And I would call it repeatedly.

Where:

  • ^M stands for Enter and
  • ^[ stands for Esc
2
  • Thanks. I'd also use something similar, but I usually then after recording a macro use :norm@a on a range of lines. It would be nice if the macro could figure out itself how often it needs to be called.
    – pschulz
    Oct 17, 2023 at 9:38
  • 1
    @pschulz the classic way is a recursive macro where an early or late « go to next occurrence » command can fail (such as searching or the f/t commands), since failures end macro playback.
    – D. Ben Knoble
    Oct 17, 2023 at 20:18

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.