<< Run Another Macro | Table of Contents | Control Windows Media Player Macro >>

Search and Replace Macro

The Search and Replace macro has two purposes:

  1. Add search and replace capability to the applications that have no such function or have it poorly designed (e.g. Notepad)
  2. Save your common search and replace rules and re-use them whenever needed

Important: Search and replace works with plain text only. While it can process formatted text, it outputs plain text only

Search and Replace Window

To add the global search and replace function, simply add Search and Replace macro, leave all parameters default and assign a hot key, for example Ctrl + Alt + H

Once you need to search and replace, press Ctrl + Alt + H, a dialog window appears (if you have selected a text prior to pressing the hot key, it will appear in a window, otherwise feel free to paste text there):

Set a "Find what" text and "Replace with:" text. For example, let's change HK abbreviation to Hot Keyboard

Click Replace All button

Everything looks good, let's click OK and Paste to put the changed text back to the notepad window:


Find what - a pattern to search

Replace with - a pattern to replace

Regular expression - treat patterns as regular expressions (Hot Keyboard uses Perl-Compatible Regular Expressions). For example, to replace decimal delimiter in numbers from comma to dots, use this rule:

Ignore case - ignore the characters case when searching for a pattern, thus "hk" matches "HK", "Hk" and "hk"

Replace all - perform replacement on all occurences found

Replace - find replace the next occurence

Find next - find the next occurence (no replacement will be made)

Undo - click this button to undo the last replacement. Please note that you cannot undo with this button if you edit text directly in the edit box. Hot Keyboard undo buffer holds up to 20 replaces.

OK and Paste - accept the replacement and paste the changed text back to the application

Cancel - cancel replacement, no changes applied

Replace according to the rules

This method is extremely useful when you need to apply standard text replacements often - you can create a macro and then re-use it when needed. It copies the selected text to the clipboard, makes replacement and pastes the text back into application.

Hot Keyboard provides very strong and advanced search and replace engine, and that's great for advanced users. However, those who don't need several interactions with regular expressions can use it too.

Before explaining every button and option, let's make a simple text replacement macro:

  • Click New rule button
  • Enter search and replace phrases and click OK
  • Set a hot key to execute the macro, for example, Ctrl + Alt + N
  • Then open Notepad and write the following text and select it:
  • Now, press Ctrl + Alt + N, you will get:

From now on, you can use Ctrl + Alt + N in e-mail editor, web forms or other programs, it will read selected text and replace all occurences of HK with Hot Keyboard


Macro mode: Replace according to specified rules - when executed, the macro will apply the replacement rules

Macro mode: Extended find and replace dialog - when executed, the macro will display a common Search and Replace dialog window, so you can set a replacement rule at the time of execution. Thus you can get search and replace function even if application does not support it or support poorly

Iteration limit - if you make a loop of rules, Hot Keyboard will break after the given number of iteration. This is to avoid infinite loops

Iteration limit defines the maximum number of replacements that can be made during execution of this macro. If this limit is exceeded, Hot Keyboard stops macro execution and shows an error message. This prevents Hot Keyboard from hanging in case of an endless replace loop due to invalid rules combination.

String length limit defines the maximum source text length in kilobytes. If the replacement makes the source text longer than this limit, Hot Keyboard stops the macro execution and displays an error message. This prevents Hot Keyboard from running out of memory in case of a very long source string.

Allow Paste Text extended syntax - check this option to enable additional Paste Text features like inserting date and time, content of a file, etc. That is, if you check this option and the result text being pasted contains, for example, ${{ FormatTime ("%x %X") }}, Hot Keyboard will paste the current date and time. Normally you should not check this option. Please refer to the Paste text documentation for the full list of available special values.

Use Paste Text 'Play keys' mode - Hot Keyboard will type the text back into the application instead of using clipboard paste function.

Source text you can type any text here to test the macro being edited.

Result text shows the result text after all transformations. It can be updated automatically (if you check the Automatically update box) soon after you modify the source text or change a rule. Otherwise it must be updated manually by clicking the Update button.

Regular expression - if checked, text in the Find what field is treated as a Perl-Compatible Regular Expression, so you can search not just for an exact Find what text but for a regular expression match.

Ignore case - if checked, Hot Keyboard does not distinguish between uppercase and lowercase characters in the source text.

Replace all entries - replace all occurrences of the search text

If this rule matches... - what to do next, if the rule has been matched and applied:

  • Stop macro execution - obvious
  • Continue with the next rule - if there is a next rule available, Hot Keyboard will try to apply the next rule
  • Restart from the first fule - loop back to the first rule. Note, that total number of loops cannot exceed Iteration limit set for the macro

The replacement text

When not using regular expressions (plain text mode), the replacement text is an ordinary text string too. No special characters or advanced features are available.

In case of a regular expression mode, the replacement text can contain the following special variables:

$n - reference to a part of the source string that matched the n-th group in the regular expression. Special case $0 denotes the part of the source string that matched the entire expression. The ${n} form is also available to delimit special variable from the following digits.

${nU} means exactly the same as ${n} but the matched part is converted to the uppercase before replacement.

${nL} - the same as ${nU}, but conversion to the lowercase is done instead.

Also, the following special characters are available:

\$ - the dollar sign;
\\ - backslash;
\r - the carriage return (CR, 0x0D) symbol;
\n - the line feed (LF, 0x0A) symbol;
\t - tabulation (0x09);
\xHH - an ASCII char with hexadecimal code HH.

Please note that most Windows applications use the combination of \r\n symbols for the line break.

More details and comments

Hot Keyboard applies replacement rules in the ascending order, beginning from the first one. If the source text matches the current replacement rule, replacement is made and Hot Keyboard continues with other replacement rules or finishes macro execution depending on the continue flag state for this rule (see below for the flags section). If a continue flag is set, Hot Keyboard increases its internal iteration counter (which is set to zero when Hot Keyboard executes the macro) and proceeds to the next or first replacement rule, thus allowing iterational replacement. Internal iteration counter prevents Hot Keyboard from hanging up in case of an endless replacement loop due to invalid rules combination. Also, Hot Keyboard checks the source string length after each replacement to prevent running out of memory if the source string becomes too long. The iteration and string length limits are not fixed and can be set by the user.

Execution of the macro ends when:
- The source text does not match any replacement rule, or
- The source text matches certain replacement rule and the continue flag for that rule is set to Stop macro execution state.


Description of flags in the rule list

R - the RegExp (regular expression) flag is set;
G - the Replace All (global) flag is set;
I - the No Case flag is set;
Cf - the Continue flag is set to restart from the first rule and
Cn - the Continue flag is set to continue with the next replacement rule.

Usage ideas:

  • Easily perform uppercase and lowercase modifications in any text editor, for any part of the text
  • Replace common misspellings - all at once
  • Use Find and Replace features in applications that normally doesn't provide that capability
  • Convert multiline paragraphs to a single text line
  • Create macros to quickly comment out blocks of code in almost any programming language