Change text the text selection via a regex

Hi there

Sometimes I want to replace text in various applications via regexes. I wrote a macro using the Search and Replace System Clipboard Using Regular Expression (ignoring case) and that works fine:

  1. In the target app I select the text (usually the whole text in an input field, so cmd-A).
  2. I copy to the clipboard (cmd-C)
  3. I call the macro. (ctrl-opt-cmd-X)
  4. I paste the clipboard. (cmd-V)

This is reasonably easy, but I was wondering whether there is a quicker way operating directly on the text selection instead of operating on the clipboard. This would save me the cm-C/cmd-V dance. I searched in the actions for "selection" and looked in the user manual for "text selection" but didn't find anything.

So, is it possible to do a Search and Replace on the selected/highlighted text?

Thanks for any pointers!

No, unless it's an app like BBEdit or the Office apps that let you manipulate the selection via scripting, going through the clipboard is generally the only way. However, you can essentially replicate the ability to operate directly on selected text just by adding copy and paste actions directly into the macro itself, which would simplify your workflow to

  1. Select the text
  2. Call the macro
1 Like

Good idea! Works perfectly!

I use a Named Clipboard as storage - and was hoping that it wouldn't overwrite the normal clipboard content - but it seems to do this. Any way to avoid this?

I have attached the macro: Change selection by regex.kmmacros (2.4 KB)

No need to bother with named clipboards. The last step you're missing is two "Delete Current System Clipboard" actions at the end, which erases the initial copy and the changed copy and essentially makes it as if you'd never copied anything to begin with.

1 Like

Mmmmh, interesting. Thanks for the follow-up.

In order to avoid Named Clipboards I was looking for an action "Copy to the System Clipboard", but couldn't find it...

There are regular "Copy" and "Paste" actions for working with the system clipboard. As an example, here's a macro I use for my own purposes to transform selected text:

Super-duper cool! That's exactly what I want. Thank you very much!

I didn't know that when KM sets the System Clipboard the old content is saved behind the scene in stack. - Is this a macOS functionality or a KM-specific thing?

No problem. The clipboard history is KM-specific, though there are also other apps that either include it or specialize in it. You can find out more about KM's implementation here: https://wiki.keyboardmaestro.com/manual/Clipboard_History_Switcher

Just out of interest, Launchbar has a very similar clipboard history capacity. I use both that and the Keyboard Maestro one. They don't work in exactly the same way, pretty similar though. The Keyboard Maestro one is best and really useful I find now. I only started using it a month or so ago, I hadn't found it either. Now I would be at a loss without it.

Interesting. I use Alfred, which probably has a mechanism for this as well - and also Copied for the (not very reliable) iOS syncing of the clipboard.

Feels somewhat pretty redundant that the same stuff gets stored in so many different places...

1 Like