How to replace a word with a predefined replacement? (many potential words)

Here is my use case: I have a list of words, and their corresponding replacements (around 10,000 different ones).

I would like to write a macro that takes a word under the cursor and replaces it with the corresponding one. This replacement will not be done in bulk.

Is there something already in KM to do something like this (perhaps an action)?

thank you,

That would depend on;

  1. The format of the word list (you would need to show us an example), and
  2. which editor you are using.

I want it to be generic: work on any application, basically, a simple text replacement tool (think of it as a large collection of abbreviations)

For example, I have a field where I need to replace text ABC (e.g. select ABC) and then run macro to replace it with DEF.

The list can be created in any format (csv, json, plain text, etc).

Are you implying that you will press a hotkey when the mouse is over the word, or are you implying that you want the solution to be constantly checking the word under the mouse even with no hotkey being pressed?

If you are willing to actually double click the mouse on the word, I think there could be a very simple solution. Here's a simple solution that will replace "teh" by "the" if you double click on any "the" but does not change any other word that you double click on. No hotkey is required in this macro, all you have to do is double click in the word that you want validated/checked and/or changed. It's simple and you might be satisfied with its approach.

If you like how it works on this simple example then we can probably adjust it to work on your 10,000 data items. Generally I find that shell commands like the one I used above are efficient even with thousands of bytes of data, so I'm not worried about that being a problem here.

CP's solutions are usually more efficient and cleaner than mine, but sometimes simple solutions like this one can be satisfactory too.

thanks for the reply.

I think you are in the right direction: copy the currently selected word to the clipboard, run script, get new word, replace the clipboard with that word, and paste the word.

so what I need to do is write such script (since I have 10,000 potential words and their replacements)

thank you

I tested it for 15 seconds and it was good enough. It's not perfect, but part of the problem is macOS's limitations on dealing with text.

If you liked my general approach, we can work with you to get an efficient solution. My example was for one word only. Making it work with 10,000 words requires a little knowledge of shell programming.

I think what you will have to do is either put the replacement commands into a file or the "word" into a file. Shell commands can handle either case. But in this case it makes much more sense to put your replacement commands into the file, since it is larger and more stable.

Here is what the replacement action would best look like: (I used a filename and location that you can change.)

image

And if you do that, your file should look like this:

s/Word1/Word2/
s/Word3/Word4/

Yes, it means you have to start each line in the file with "s/" and end each word with "/" but that's not a huge price to pay.

Make sure that your words are not subsets of each other. Something like this might be problematic: (because "Light" is found in "Lightning").

s/Light/Bright/
s/Lightning/Thunder/

Oh, I just realized you might need to use the "i" flag on the sed command in order to make the replacements case-insensitive.

This, for example, seems to work in a few editors, and should scale for much larger JSON dictionaries of this pattern:

Lexical alternative pasted for word containing cursor.kmmacros (5.8 KB)


Originally mis-posted an indicative example to its own thread:

This is the big problem -- your "a word" isn't always the Mac's "a word". For example, you may consider the two parts of a hyphenated word to be a whole but your Mac considers them to be two words with a hyphen between.

Given that, you may prefer to do a manual selection then run your "replace" macro rather than relying on a double-click or the Option-arrow dance to get your selection.

1 Like

Great point. We don't know what his dictionary contains, so that may be a problem.