FAQ: Automating Notepad++
-
Users often question whether Notepad++ has scripting or other automation capabilities available.
Natively, Notepad++ has Macros functionality, which allows you to record various Notepad++ menu commands, including search-and-replace commands.
If you need more power than macros provide, and have some skill with programming, then there are many Notepad++ Plugins that provide the power of a full programming language with an interface to the Notepad++ menus and the file buffers that are open inside Notepad++. And if that isn’t enough, you can also do external remote-control of Notepad++.
Macros
The Notepad++ built-in macro facility allows recording Notepad++ actions (see next paragraph for details), and playing them back once or repeatedly; macros can be just for a single session, or saved so that they are available every time you run Notepad++. The Online User Manual includes instructions on how to use the Macros system, along with a technical discussion of format for saved macros in the config files so that you can manually edit an existing macro or create one from scratch, so that information will not be repeated here. Any saved macro can be easily given a keyboard shortcut when it is saved or by using Notepad++'s Shortcut Mapper for an already-existing macro.
The kinds of “Notepad++ actions” that can be recorded include:
- Typing text (characters) in the active document
- Using arrow keys, pageUp, pageDown, Home, and End keys to move the typing caret in the active document
- Selecting and copying/cutting/pasting text.
- Executing most Notepad++ menu commands
- “Constant” Search and Replace actions
- when you use a macro to record a search or replace action, the FIND and REPLACE fields will be “constant” in the macro, so they will always be the same FIND and REPLACE values every time you run the macro
For many, Macros are sufficient for their automation needs, especially because they can automate multi-step search-and-replace sequences and the like. However, there are some limitations on Macros that might help you decide to move on to something more powerful than the built-in Macros system:
- Some Notepad++ menu commands are not available to macros:
- Some cannot be saved in a macro because they require user input, which the Macros feature doesn’t support (so actions like Save As are not available).
- Some used to be available, but after an EU-FOSSA review some years ago, they were removed from the macro-recording capability due to their potential for abuse by malicious macros.
- Some can be used by macros, but due to technical difficulties are just not recorded when you try to record a macro. On those, if you manually edit or create a macro by editing the
shortcuts.xml
config file, you can still use them. - If you run across a command that doesn’t get recorded when you are making a macro, you can try to manually edit the macro to include that command. If it works, great. If not, then unfortunately, your needs may be outside of a macro’s capabilities.
- The FAQ author has not come across a reference list that enumerates every Notepad++ command and whether it can be recorded in a macro or just played back in a manually-created macro or cannot be used at all in a macro. If you know of such a list, or are willing to create it, feel free to contact the FAQ author, or put in a pull request to update the GitHub copy of this FAQ.
- The Macro language is not a full programming language:
- It has no variables.
- It has no looping capability (other than playing the full macro N times).
- No conditional execution (“if/then/else” or similar constructs).
- It has no method of user input when the macro is running, so you cannot prompt for a file name or for text to be used for a complicated interaction.
- This has the implication that searches that you record in a macro will always search for the same text; you cannot search for different text every time you run a particular macro.
- It has no concept of time, so you cannot say “do X, wait T seconds, then do Y”
Plugins
There are a variety of plugins which embed a full programming language in the Notepad++ environment, which gives scripts in that plugin the ability to access Notepad++ command and perform actions on the files open in Notepad++, with the full power of a programming language (with variables, looping, conditionals, user input, and libraries that are available for that language).
The plugins linked below can all be installed through the Plugins Admin interface. The first two get more mentions in the Community, because they are the ones that many of the regulars use, but all are reasonable choices for Notepad++ automation through scripting.
- NppExec - This was one of the earliest “scripting” languages, which provides capabilities similar to a Windows batch file, but is more powerful. This is great if you are wanting to automate saving, file-system operations, and the like; it is also frequently used for gluing together a process using linters, code-cleaners, compilers, and even running the code in a console in Notepad++.
- PythonScript - Provides Python-based scripting (for now, Python 2.7 for the default PythonScript 2 installed via Plugins admin, though a Python 3.x interpreter is available by using one of the 3.0.X “alpha” pre-release versions that you can download at GitHub).
- PythonScript solutions are so common in the Community that we have an entire FAQ entry devoted to installing and using scripts in PythonScript.
- ActiveX Plugin - Allows you to control Notepad++ via ActiveX.
- Automation Scripts - This provides C#-based scripting.
- jN Notepad++ Plugin - This provides JavaScript-based scripting.
- LuaScript - This provides Lua-based scripting.
There may be other scripting plugins that aren’t available in the Plugins Admin (and possibly even ones that are listed there, but they haven’t come up in the Community or aren’t described in the Plugins Admin in a way that makes it obvious that they include Notepad++ automation capabilities).
Some users may argue that “if it requires a plugin, then Notepad++ doesn’t really support scripting”. The FAQ author and many Community members disagree: the Notepad++ Developer seems to have chosen to “support” scripting by encouraging the creation of and use of scripting plugins that internally use the plugin API that Notepad++ provides. The Developer seems content to not “compete” with plugins by adding functionality that one or more plugins already successfully implement (this also frees up his time to focus on the core of Notepad++, rather than spending his time on maintaining and supporting one or more scripting languages, which is not where his interests apparently lie).
External Automation
Any external application that can send keystrokes to a chosen application or use the Win32 API’s
SendMessage
interface can control.Some external tools known to be able to control Notepad++:
- AutoHotKey - This is a popular automation tool used on Windows, and Community members sometimes share AHK scripts to perform Notepad++ automation tasks.
- Win32::Mechanize::NotepadPlusPlus - This module for the Perl programming language supplies an interface similar to the scripting plugins, using a copy of Perl installed externally to Notepad++.
- Though some Community members kindly refer to it as “PerlScript” due to it’s similarity to the “PythonScript” plugin, it is not a plugin. The biggest drawback is that not include the menu interface provided by the scripting plugins, so you cannot easily choose or create or edit a script from the Notepad++ menus.
-
-