KM 9 Issue with Old Macro (RegEx/new line replacement)

I have used a macro for a long time that takes whatever text is highlighted, searches and replaces the linebreaks with the > character, for quotation purposes in apps like ZenDesk etc.

Make Quote.kmmacros (5.5 KB)

However, having upgraded to KM9, this no longer works as expected. I'm not entirely sure why, and I haven't been able to find an updated replacement. Could somebody advise/assist?

Since I don't have KM v8 any more, I can't figure out what your macro is supposed to do because you said your macro doesn't work in v9. The only way I can figure out what your macro is supposed to do is to read the text in your post. And all I see in the post is "replace line breaks with > character". but I think that's not all you want to do because your macro looks much more sophisticated than that.

If you are willing to explain in the post exactly what you are trying to do, I can help. I enjoy text processing problems.

If all you are trying to do is replace line breaks with > that's easy:

P.S. I don't have Zendesk so I really can't infer from that part of your post what you are trying to do.

Thanks for your reply!

Yes. I want to take something like the following:

This is a test line
Test 2

Highlight it, then have it replaced with:

> This is a test line
> Test 2

so essentially just add the > as a prefix for each new line.

I tried the Macro you outlined above, but it returns the following:


so close... but not quite there!

With a bit of investigation, it seems like I need to figure out/use a sed command, which I haven't used before. Will dig in a bit and see if I can get that working, unless someone can point me the right way!

This is easily done using the KM Search and Replace action

Example Output


MACRO:   TEST Regex Replace Syntax

**Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+**
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

#### DOWNLOAD Macro File:
<a class="attachment" href="/uploads/default/original/3X/f/e/fea597a60bbd299be2c1500d498a8e325c0563ba.kmmacros">TEST Regex Replace Syntax.kmmacros</a>
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**


<img src="/uploads/default/original/3X/f/f/ff44533135e1349ade7524da1cc92f577f016ee4.png" width="528" height="703">

Thanks! I enabled this and tried it out but it didn't seem to do anything. I had the same issue when trying Search and Replace earlier today. Not sure what the deal is there.

Make sure that you:

  1. Enable both the Macro and the Macro Group it is in.
  2. That the Macro Group is ACTIVE when you trigger the macro
  3. Run the Macro -- do NOT try to "try" an Action.

It runs fine on my Mac, running Keyboard Maestro 9.0.2 on macOS 10.14.5 (Mojave).

Weird. I thought everything was already active, but created a new macro group and it worked.

I tried to adapt it to take what was on the clipboard, but it seems to just paste the same thing:

Does anything stand out as obviously wrong there? The idea is to be able to highlight a paragraph, hit the hotkey and then have the text replaced with the > on each line.

Thanks again for all of your help!

For anyone following along, I ended up using...

awk '{print "> "$0}'

with @Sleepy's macro, and it works!

I just came back from dinner. Your solution looks quite efficient. It should be fine. But I recommend trying to get JM's solution to work because it probably uses less macOS resources. I'll leave it between the two of you because he's an elite wizard and it's his macro.

This is wrong:

It's very simple. Replace the above AND the Type a Keystroke with:


Or if you want to CUT instead of Copy, then use the KM CUT Action.


EDIT: 2019-09-14 20:06 GMT-5

Are you taking advantage of the helpful tools that KM provides?

The reason the macro does not work is that it is using $2 in search & replace cases where there is no second capture group. I'd guess that in previous versions, that is probably just returning an empty string, but it was never legitimate.

All you should need for this conversion is this Search & Replace:


(although I just noticed that is leaving the result as rich text even if the source was only plain text, which I will resolve).

Whose post/macro are you referring to? I don't see "$2" used anywhere.

I used this above, and it worked fine:


He was referring to the original post. There is a macro in that post.

The OP post macro. “having upgraded to KM9, this no longer works as expected” - because it should never have worked as expected (I'm not really sure what the $2 would event be intending to mean in a regex with only one capture).

Thanks for all the help. I have no idea what the original macro's logic was... I set it up years ago and forgot about it, so your guess was as good as mine. I am not really a skilled scripter/programmer, so I just muddle through until it works.