[macros] How to write macro for Notepad++?

I would like to write a macro for Notepad++ which should replace char1, char2, char3 with char4, char5, char6, respectively.

This question is related to macros notepad++

The answer is


I'm not sure if this helps, but I needed to create a macro to hold a snippet, so I simply recorded myself inserting the items and set a shortcut to it. Granted, I'm not using version 5.9 so there might be some slight version differences. To access the macro recorder go to Macro > Start Recording. Then you will perform your action and then go to Macro > Stop Recording. I'd recommend playing it back to ensure it's correct and then save and set your shortcut key.

Hope the helps.


This post can help you as a little bit related :

Using RegEX To Prefix And Append In Notepad++

Assuming alphanumeric words, you can use:

Search = ^([A-Za-z0-9]+)$ Replace = able:"\1"

Or, if you just want to highlight the lines and use "Replace All" & "In Selection" (with the same replace):

Search = ^(.+)$

^ points to the start of the line. $ points to the end of the line.

\1 will be the source match within the parentheses.


I'm not sure if this helps, but I needed to create a macro to hold a snippet, so I simply recorded myself inserting the items and set a shortcut to it. Granted, I'm not using version 5.9 so there might be some slight version differences. To access the macro recorder go to Macro > Start Recording. Then you will perform your action and then go to Macro > Stop Recording. I'd recommend playing it back to ensure it's correct and then save and set your shortcut key.

Hope the helps.


I found 'Python Script' plugin for Notepad++ more useful since with the plugin, I could write simple macros in the form of python and It has also got very good documentation and sample macros written in python as well. If you are quite comfortable with python, then I think 'Python Script' will provide justice. For more information, refer : http://npppythonscript.sourceforge.net/


My personal experience is that shortcuts.xml is overwritten with the initially loaded + later recorded macros and settings when Notepad++ exits. So you can't use Notepad++ itself for editing this file.

Close Notepad++, edit shortcuts.xml by another tool, save it and restart Notepad++.


Actually, the shortcuts.xml file does not store user-generated macros and no obvious candidates contain this info. These instructions are out of date.

Contrary to various websites' tips, storing user-generated macros isn't enabled for v.5.4.2. That XML file is there, but your macro does not get stored it in.

I'm guessing it's a bug that'll be fixed by the next version.


I just did this in v5.9.1. Just go to the Macro Menu, click "Start Recording", perform your 3 replace all commands, then stop recording. You can then select "Save Current Recorded Macro", and play it back as often as you like, and it will perform the replaces as you expect.


This post can help you as a little bit related :

Using RegEX To Prefix And Append In Notepad++

Assuming alphanumeric words, you can use:

Search = ^([A-Za-z0-9]+)$ Replace = able:"\1"

Or, if you just want to highlight the lines and use "Replace All" & "In Selection" (with the same replace):

Search = ^(.+)$

^ points to the start of the line. $ points to the end of the line.

\1 will be the source match within the parentheses.


Posting more than 10 years after the OP, but I think that this is still a relevant question (it is for me, at least). Today, there is quite some information in the Notepad++ User Manual, relating to the OP's question:

Here is a block of macro code: replace SEARCHTEXT by REPLACETEXT, using regular expressions, "." finds /r and /n, in every file matching the filter GLOBFILEFILTER in a folder PATH (no subfolders, not sure where this flag is defined/set).

<Macro name="REPLACE_IN_FILES_REGEX_DOT_FINDS_CR_AND_LF" Ctrl="no" Alt="no" Shift="no" Key="0">
    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
    <Action type="3" message="1601" wParam="0" lParam="0" sParam="SEARCHTEXT" />
    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
    <Action type="3" message="1602" wParam="0" lParam="0" sParam="REPLACETEXT" />
    <Action type="3" message="1653" wParam="0" lParam="0" sParam="PATH" />
    <Action type="3" message="1652" wParam="0" lParam="0" sParam="GLOBFILEFILTER" />
    <Action type="3" message="1702" wParam="0" lParam="1024" sParam="" /> 
        <!-- #COMMENT: "1024" seems to be the flag ". finds /n and /r". This is not in the documentation. -->
    <Action type="3" message="1701" wParam="0" lParam="1660" sParam="" />
</Macro>

Remarks about this code:

  • The path to the folder needs to end with a backslash.
  • Characters like <, >, & need to be escaped in the xml. Rather create your search and replace strings by recording a macro.

I recorded a macro and I found it in %APPDATA%\Notepad++\shortcuts.xml. It looks like posted in the first post of this thread.

I use NPP Ver. 5.9.6.2 with Win7.


Actually, the shortcuts.xml file does not store user-generated macros and no obvious candidates contain this info. These instructions are out of date.

Contrary to various websites' tips, storing user-generated macros isn't enabled for v.5.4.2. That XML file is there, but your macro does not get stored it in.

I'm guessing it's a bug that'll be fixed by the next version.


I found 'Python Script' plugin for Notepad++ more useful since with the plugin, I could write simple macros in the form of python and It has also got very good documentation and sample macros written in python as well. If you are quite comfortable with python, then I think 'Python Script' will provide justice. For more information, refer : http://npppythonscript.sourceforge.net/


This post can help you as a little bit related :

Using RegEX To Prefix And Append In Notepad++

Assuming alphanumeric words, you can use:

Search = ^([A-Za-z0-9]+)$ Replace = able:"\1"

Or, if you just want to highlight the lines and use "Replace All" & "In Selection" (with the same replace):

Search = ^(.+)$

^ points to the start of the line. $ points to the end of the line.

\1 will be the source match within the parentheses.


Posting more than 10 years after the OP, but I think that this is still a relevant question (it is for me, at least). Today, there is quite some information in the Notepad++ User Manual, relating to the OP's question:

Here is a block of macro code: replace SEARCHTEXT by REPLACETEXT, using regular expressions, "." finds /r and /n, in every file matching the filter GLOBFILEFILTER in a folder PATH (no subfolders, not sure where this flag is defined/set).

<Macro name="REPLACE_IN_FILES_REGEX_DOT_FINDS_CR_AND_LF" Ctrl="no" Alt="no" Shift="no" Key="0">
    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
    <Action type="3" message="1601" wParam="0" lParam="0" sParam="SEARCHTEXT" />
    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
    <Action type="3" message="1602" wParam="0" lParam="0" sParam="REPLACETEXT" />
    <Action type="3" message="1653" wParam="0" lParam="0" sParam="PATH" />
    <Action type="3" message="1652" wParam="0" lParam="0" sParam="GLOBFILEFILTER" />
    <Action type="3" message="1702" wParam="0" lParam="1024" sParam="" /> 
        <!-- #COMMENT: "1024" seems to be the flag ". finds /n and /r". This is not in the documentation. -->
    <Action type="3" message="1701" wParam="0" lParam="1660" sParam="" />
</Macro>

Remarks about this code:

  • The path to the folder needs to end with a backslash.
  • Characters like <, >, & need to be escaped in the xml. Rather create your search and replace strings by recording a macro.

My personal experience is that shortcuts.xml is overwritten with the initially loaded + later recorded macros and settings when Notepad++ exits. So you can't use Notepad++ itself for editing this file.

Close Notepad++, edit shortcuts.xml by another tool, save it and restart Notepad++.