Find and Replace Text Issue Using the Clipboard and Selected Text

Hi KM'ers!

I am trying to do a find and replace in selected text as follows:

  1. Copy text to be replaced from a larger document to system clipboard, eg. 4 spaces.
  2. Select entire text i want to run find and replace on.
  3. Run macro and get a prompt for "find" and one for "replace". Here i want to paste in the first 4 spaces i copied.
  4. Click ok and text gets replaced.

I noticed that Copy to named keyboard overwrites the system keyboard in this thread from 2015: Named Clipboard Question

And have been unable to create a flow that allows me to paste in the initial system clipboards "4 spaces".

Any help is greatly appreciated!

Here is a screenshot of one of the workarounds I have tried and the resulting macro that does not work:

Quick Find and Replace.kmmacros (3.4 KB)

If I remember correctly, space characters will be trimmed at the begging and the end of a string. Could this be the cause?
If you would like to replace for four spaces, use regex search and put ░{4} (where ░ represents a space character) there may help.

I have a very similar macro that I made, using RegEx search:

30)Search and Replace Regex in SystemClipboard.kmmacros (2.8 KB)

I have another macro for string search:

31)Search and Replace String in SystemClipboard.kmmacros (2.8 KB)

Thank you Martin, the issue is that I can't:

  1. copy what i want to replace, for example "ANYSTRING", then
  2. select text i want to perform the action on, then
  3. paste the text "ANYSTRING" into the 'search for' field
  4. click ok and perform find and replace

If you see my attached macro it does exactly what your second one does, it just attempts to get around the system clipboard overwrite issue,

Thanks!

Hey Martin,

Don't use a named clipboard for the find string. Use the %SystemClipboard% token in the find field of the dialog, so you don't have to paste it manually.

Or you can use a variable instead of the token.

-Chris

For example:

System Clipboard to Find & Replace User-Prompt v1.00.kmmacros (7.1 KB)

Keyboard Maestro Export

After a more careful reading, I think I finally got what you meant.

In that case, I think you may simply add "delete current clipboard" action after the copy to named clipboard action.

Thanks y'all! I was able to go down a modified "past clipboard" route. Seems robust:

MT Find and Replace v1.kmmacros (3.3 KB)

3 Likes

When you delete the past clipboard 0, clipboard 1 will become clipboard 0, so the effect is the same with setting systemclipboard with clipboard 1.
The difference is that with the latter, the original clipboard 0 (what you copied to the named clipboard) moved to clipboard 1, whereas with the former, the original clipboard 0 is simply deleted. I thought the former better suits your case, because you do not seem to need the content of the named clipboard to show up in the clipboard history at all. But this is a minor issue and does not affect your need.