How do I do several replaces in one shot?

Posted by Emmanuel ® , 02/23/2024, 06:02:34 Reply   Forum

Selection commands

The Selection Commands are very good at transforming selected text. The most popular transformations consist in uppercasing, lowercasing, title casing or replacing one word with another.

The following expansion, for example, selects all and replaces all occurrences of centimeters by cm.

{Ctrl A}{Wrap}
{ReplaceAll}
wholeWord = "y"
matchCase = "n"
preserveCase = "n"
findWhat = "centimeters"
replaceWith = "cm"
{/ReplaceAll}

As you can see it produces the {Ctrl A} keyboard shortcut for select all, followed by a ReplaceAll command.

Now let us try to create an expansion that replaces all occurrences of centimers by cm and milligrams by mg.

Why Using Two Consecutive "Replace All" Commands Will Not Work

It is quite natural to want to use two consecutive ReplaceAll commands as follows:

{Ctrl A}{Wrap}
{ReplaceAll}
wholeWord = "y"
matchCase = "n"
preserveCase = "n"
findWhat = "centimeters"
replaceWith = "cm"
{/ReplaceAll}{Wrap}
{ReplaceAll}
wholeWord = "y"
matchCase = "n"
preserveCase = "n"
findWhat = "milligrams"
replaceWith = "mg"
{/ReplaceAll}

To understand why it is a pitfall it is important to understand that a ReplaceAll command:

{ReplaceAll}...{/ReplaceAll}

is indeed equivalent to:

{CopySelection}{Wrap}
{ReplaceAllClipboard}...{/ReplaceAllClipboard}{Wrap}
{Paste}

So that 2 consecutive ReplaceAll commands:

{ReplaceAll}...{/ReplaceAll}{Wrap}
{ReplaceAll}...{/ReplaceAll}

are equivalent to:

{CopySelection}{Wrap}
{ReplaceAllClipboard}...{/ReplaceAllClipboard}{Wrap}
{Paste}{Wrap}
{CopySelection}{Wrap}
{ReplaceAllClipboard}...{/ReplaceAllClipboard}{Wrap}
{Paste}

What happens if we produce the above expansion?

  1. CopySelection copies the selected text to the clipboard.
  2. ReplaceAllClipboard performs the first requested replace on the clipboard content.
  3. Paste pastes the clipboard content thereby replacing the selected text.
    At that stage it is too early to paste the transformed text back: only the first replace has been done.
  4. The next CopySelection simply fails because there is no selected text anymore.
  5. ReplaceAllClipboard performs the second requested replace on the clipboard content.
  6. Paste pastes the clipboard content.

Hence 2 Paste commands will be produced and the result will not be what we expected.

The Solution: Use Consecutive "Replace All Clipboard" Commands

After understanding why two consecutive ReplaceAll commands fail to produce the expected result, the solution becomes clear. Simply group the ReplaceAllClipboard commands between the CopySelection and Paste commands as follows:

{CopySelection}{Wrap}
{ReplaceAllClipboard}...{/ReplaceAllClipboard}{Wrap}
{ReplaceAllClipboard}...{/ReplaceAllClipboard}{Wrap}
{Paste}

So if you want to perform the following replaces:

  • centimeters by cm
  • milligrams by mg
  • period by .
  • comma by ,

with one expansion, you will use:

{Ctrl A}{Wrap}
{CopySelection}{Wrap}
{ReplaceAllClipboard}
wholeWord = "y"
matchCase = "n"
preserveCase = "n"
findWhat = "centimeters"
replaceWith = "cm"
{/ReplaceAllClipboard}{Wrap}
{ReplaceAllClipboard}
wholeWord = "y"
matchCase = "n"
preserveCase = "n"
findWhat = "milligrams"
replaceWith = "mg"
{/ReplaceAllClipboard}{Wrap}
{ReplaceAllClipboard}
wholeWord = "y"
matchCase = "n"
preserveCase = "n"
findWhat = "period"
replaceWith = "."
{/ReplaceAllClipboard}{Wrap}
{ReplaceAllClipboard}
wholeWord = "y"
matchCase = "n"
preserveCase = "n"
findWhat = "comma"
replaceWith = ","
{/ReplaceAllClipboard}{Wrap}
{Paste}




Edit | Reply | | |   | Current page | Author