Does anyone make use of macros?

Just curious what everyone’s experience is. I came from a couple years of using vim, where I had gotten used to using macros to repeat complicated edits on multiple lines. However, with kakoune’s powerful multiple selections, I find that same use case is handily covered, and a lot more interactive and easy to get right. To date I have not had any reason to press q in normal mode in Kakoune.

Macros are useful for irregular code or when I know I will need to repeat something later or on multiple buffers.

Another nice use-case: when I create custom plugins/command instead of writing execute-keys sequences manually I record a macro and insert key sequence to my script with ctrlr@

3 Likes

I also use macros when I want to pause my brain: instead of doing a complex change on a large region, I record a macro for a tiny portion where I’m sure it works, and repeat in multiple places to observe the changes.

If you ever need to edit a macro, you can do:

  1. :set-register a '<c-r>@, edit and validate,
  2. Q"aq, and continue recording.
1 Like

This Pull Request can help a bit while creating macros: https://github.com/mawww/kakoune/pull/2104

1 Like

I use macros occasionally, when multiple selections don’t cut it. But I think they could be a lot more powerful if they worked better with multiple selections. Such as running in a disposable context for each selection.
As an example, I tried to do this the other day:

class Foo:
    __tablename__ = 'foo'
    foo = Column
    otherfoo = Column

class Bar:
    __tablename__ = 'bar'
    bar = Column
    otherbar = Column
    superbar = Column

For each class, make a comma separated line containing the class column names below that class.
You can’t do that with multiple selections, and using a macro normally, I have to go select each class and run it, for the N classes in my file. I’m lazy and don’t want to have to do that.

This was the solution I came up with.

Select all classes and save selections to ^

%S\n\n<ret>_Z

Select one class and record macro

<a-i>pQsColumn<ret>giey]pk<a-p>a,<space><esc><esc>

Undo, then restore selections and run macro for each selection (‘q’ needs to be whatever you mapped the macro key to).

z:exec -itersel -with-hooks -with-maps 'q'<ret>

And all at once, it’s done!

Another way I wish macros could work better with multiple selections, is to run each selection in sequence, as if 14q were typed to run the macro 14 times in succession, for 14 selections. This way the macro can build off previous iterations of itself. A simple example would be incrementing a list of numbers, but IDK how I’d implement that.

1 Like