To use Python to extend notepad++, you first need to install the Python script plugin for notepad++, and the notepad++ plugin installation method can refer to here.
After installing the Python script plugin, you will see the Python script menu under the Plugins menu.
Python Script Introduction
Click the Plugins->python script->new Script menu to open the directory where the user's Python script resides. Once the Python script is placed in the directory, a menu with the same name as the Python script will appear in the scripts directory under Python scripts, and the corresponding Python script can be executed by clicking on the menu.
For example, I placed two scripts in this directory:
In the Scripts directory, the following is:
Click Show Console to open the Python console, which displays the version number of the python used, and the Python script can output the information to the console, which will be described in more detail later.
Context-help is the help document for Python script, which includes an introduction to the API, which is often used later.
NPP Module Introduction
The NPP module contains Notepad, editor, and console objects, which are used to manipulate Notepad++,notepad objects as instances of the Notepad class, and editor is an instance of the editor class. The console object is an instance of the console class.
The NPP module has been introduced in the startup.py script:
From NPP Import *
Since the startup script and your script are under the same namespace, you can use them directly in your Python script, as shown in the following example.
The following is a brief introduction to the objects in NPP, a more detailed interface reference Context-help.
Console Object
Console can open the console, the console object is used to manipulate the console window, you can show or hide the console, clear the console content, output information to the console. The console also supports running a command and outputting the command result to the console, so you can call your compiler in Python script or run any command-line tool.
Here is a simple example of displaying the console and printing the information:
Console.show () Console.Write ("hello,world!")
Saving the above script to the specified directory and executing the script under the Scprits menu will automatically open the console and enter "hello,world!" in the console, that is, the following effect:
Notepad object
The Notepad object represents the notepad++ itself, and you can use the Notepad object to open and save the file, choose a different tab, convert the format, run the plug-in command, and so on.
Here is an example, if you are editing a file that ends with ". Log", the timestamp is automatically added at the end of the file when the file is saved:
import datetime
def addSaveStamp(args):
if notepad.getCurrentFilename()[-4:] == ‘.log‘:
editor.appendText("File saved on %s\r\n" % datetime.date.today())
notepad.callback(addSaveStamp, [NOTIFICATION.FILEBEFORESAVE])
The callback method of Notepad is to register a time callback listener, and the event type is defined in the enumeration notification.
If you want to cancel a registered listener, then use the following method:
#Cancel all registered listeners
notepad.clearCallbacks ()
#Cancel registration listening for certain events
notepad.clearCallbacks ([NOTIFICATION.FILESAVING, NOTIFICATION.FILESAVED])
#Cancel the listening of the specified function
notepad.clearCallbacks (addSaveStamp)
#Cancel the specified function from listening to certain events
notepad.clearCallbacks (addSaveStamp, [NOTIFICATION.FILESAVED])
Editor Object
The Editor object corresponds to the text area of the notepad++ and provides methods to help manipulate the data, many of which can be implemented directly using Python, which the editor object provides just to simplify the operation.
Here is a simple example of finding the line that starts with ' # ' in the currently open file, deleting the rows, and saving the file:
for i in range(editor.getLineCount() - 1, -1, -1) :
line = editor.getLine(i)
if line.startswith(‘#‘):
editor.gotoLine(i)
editor.lineDelete()
notepad.save()
The script starts walking forward from the last line, finds the line that begins with ' # ', deletes it, and finally saves the file. The problem with this script is that if the file is not saved, a File Save dialog box pops up.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Extend notepad++ with Python