Macro to add symbol before and after text

  • Hi,

    I am working on a lot of German text which uses „this type of quotation marks“ to mark direct speech. I find it extremely tedious to copy-paste them again and again because the symbol on a standard German keyboard results in “this”, so I cannot just type it like that.

    I made a macro a year ago that would automagically add „these quotation marks“ before and after a selected text. However, due to a reinstallation of Windows, my precious macro is gone and I can’t figure out how I did it.

    Can someone point me into the right direction?

  • @JP-A

    It seems like you’d want an “in-selection” replacement. Record this as a macro:

    Search for (?s).+
    Replace with a${0}b (where a is your opening quotes, b is your closing)
    Tick In selection checkbox
    Tick Regular expression search mode.
    Press Replace All

  • @JP-A said in Macro to add symbol before and after text:

    „this type of quotation marks“

    record a macro by keyboard actions like


  • @Ekopalypse said in Macro to add symbol before and after text:

    record a macro by keyboard actions like

    Potential downsides:

    • destroys clipboard contents
    • doesn’t work for those without numeric keypad on their keyboard (and yes, there are those of us for which a keyboard with a numeric keyboard is a hinderance; thus mine):


  • What a cool forum!

    Thank you for your suggestions. I love Ekopalypse’s approach for the simplicity, but Alan is right about the clipboard issue. And I use the clipboard a ton. So I’ve tried Alan’s solution and it works beautifully.

    Thank you!

  • @Alan-Kilborn thanks for the solution! I was able to use it to create the following macro for markup coding that I will be doing frequently (among others):

            <Macro name="bold" Ctrl="yes" Alt="no" Shift="no" Key="66">
                <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                <Action type="3" message="1601" wParam="0" lParam="0" sParam="(?s).+" />
                <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                <Action type="3" message="1602" wParam="0" lParam="0" sParam="**${0}**" />
                <Action type="3" message="1702" wParam="0" lParam="898" sParam="" />
                <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />

    Unfortunately, when I do a multi-select, only the last selection is modified by the macro. Is there a way to modify the macro code to support multiple selections? For example, in the sentence I want this, this, and this to be modified., I want to be able to Ctrl+double-click each this, apply the shortcut once, and have all 3 changed to **this** (i.e. I want **this**, **this**, and **this** to be modified.).

  • @TroshinDV ,

    What does your screenshot add to the conversation? With no explanation in the text of your post, it is very hard to tell what you are trying to say. While the old saying is that “a picture is worth 1000 words”, I don’t think that’s true when there is no context given to the picture.

    It looks like you were showing an alternative to using the built-in macro language, which is fine (but unnecessary, since the OP had a solution already, which doesn’t require external installs)… but explaining that this is an alternative solution will help.

    Does your trdmBrackets.js script address the OP’s additional request of wanting to handle multi-selection? Your example screenshot only shows a single selection, and you didn’t explain, so we cannot know.

  • @mathlete2 ,

    The problem with trying to do an “in-selection” search-and-replace in a multi-selection region is that the search-and-replace engine navigates through the matches by changing the selection… which would break the multi-selection.

    In fact, if you try to do a multi-selection region and then run Search > Replace, you will see that the “☐ In Selection” checkbox is greyed out, so that you cannot use it. A multi-selection is a different beast than a normal selection, and there are some things that don’t work the same.

    It might be that you have to go deeper into one of the scripting languages (like the jN-NPP that @TroshinDV uses, or the PythonScript that many of the regulars use)… Looking through the scintilla docs, I found “Multiple Selection and Virtual Space” – these messages should all have corresponding methods in the PythonScript editor object (or equivalent in other scripting plugins). So I pieced together this script which does multi-selection replacement, where it doesn’t matter what the contents of the multi-selection are: it will just surround them by the **:

    # encoding=utf-8
    """in response to multi-selection request
    the comments show my debug code, or alternative methods
    this will loop through all N sections of the multi-selection
    for each section:
    * find the start and end locations
    * grab the original text from that section
    * replace the original by surrounding it with **
    * it allows a single undo for the whole group of replacements, but if you do UNDO,
        it will clear the multi-selection
    * at the end of the replacement, the multi-selection goes down to a zero-length
        selection at the start of each of the original words;
            shift+arrows or shift+ctrl+arrow will allow you to extend the multi-selection again
    from Npp import *
    for mSel in range(editor.getSelections()):
        selStart = editor.getSelectionNStart(mSel)
        selEnd = editor.getSelectionNEnd(mSel)
        orig = editor.getTextRange(selStart, selEnd)
        #console.write("loop #{}: {}..{} = \"{}\"\n".format(mSel,selStart,selEnd,orig))
        txt = "**" + orig + "**"

  • @PeterJones thanks for putting that together! Unfortunately, I’m not familiar with how to work such scripts into NP++. Where is this documented?

  • @mathlete2 ,

    PythonScript is a plugin, which you install through Plugins > Plugins Admin interface.

    It comes with its own documentation available thru the Plugins > PythonScript > Context-help menu entry

    This post gives a run-through for how to get the script into the plugin, and how to run the script

  • @PeterJones said in Macro to add symbol before and after text:

    What does your screenshot add to the conversation?

    I hinted a person that there is another solution. If he became interested, he would ask. And his silence indicates a lack of interest.
    Я намекнул человеку что есть и другое решение. Если бы он заинтересовался, он бы спросил. А его молчание указывает на отсутствие интереса.

  • @mathlete2 said in Macro to add symbol before and after text:

    @PeterJones thanks for putting that together! Unfortunately, I’m not familiar with how to work such scripts into NP++. Where is this documented?
    My developments are based on this plugin.

Log in to reply