Use Python extension NotePad ++ and python extension notepad

Source: Internet
Author: User

Use Python extension NotePad ++ and python extension notepad
To use Python to expand NotePad ++, you must first install the Python Script plug-in for NotePad ++. For how to install the NotePad ++ plug-in, refer to here.

After installing the Python Script plug-in, you can see the Python Script menu under the menu Plugins.

Introduction to Python Script
Click Plugins-> Python Script-> New Script menu. The opened directory is the directory where your Python scripts are stored. Put the Python Script in this directory, and a menu with the same name as the Python Script will appear in the Scripts directory under the Python Script. Click this menu to execute the corresponding Python Script.
For example, I put two scripts in this directory:

In the Scripts directory:

Click Show Console to open the Python Console. The Python version number is displayed. The Python script can output information to the Console, which will be detailed later.
Context-help is a help document for Python scripts, including the introduction of APIS, which will be frequently used later.
Npp module Introduction
The Npp module contains notepad, editor, and console objects. These objects are used to operate Notepad ++. notepad objects are Notepad instances and editor instances, 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 in the same namespace, you can directly use them in your Python script, as shown in the following example.
Next, we will give a brief introduction to the objects in Npp. For more information about the interfaces, see Context-help.
Console object
You can open the Console. The console object is used to operate the Console window. You can display or hide the console, clear the content in the Console, and output information to the console. The console also supports running a command and outputting the command result to the console. In this way, you can use Python Script to call your compiler or run any command line tool.
The following is a simple example to display the console and print the information:
console.show()console.write("Hello,world!")
Save the preceding script to the specified directory and execute the script under the scprits menu. Then, the console is automatically opened and "Hello, world!" is entered in the console !", That is, the following results:

Notepad object
The notepad object represents the Notepad ++ itself. You can use the notepad object to open and save files, select different tabs, convert formats, and run plug-in commands.
The following is an example. If you edit a file ending with ". log", a timestamp is automatically added at the end of the file when the file is saved:
import datetimedef 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 registers a time callback listener. The event type is defined in enumeration NOTIFICATION.
If you want to cancel a registered listener, use the following method:
# Cancel all registration listening notepad. cancallbacks () # cancel the notepad registration listening for certain events. clearCallbacks ([NOTIFICATION. FILESAVING, NOTIFICATION. FILESAVED]) # cancel the notepad listening of the specified function. clearCallbacks (addSaveStamp) # cancel the notepad listening of the specified function to certain events. clearCallbacks (addSaveStamp, [NOTIFICATION. FILESAVED])
Editor object
The editor object corresponds to the text area of Notepad ++ and provides some methods to help operate data. Many methods can be implemented directly using Python. The editor object only provides these methods to simplify operations.
The following is a simple example. Find the rows starting with '#' in the currently opened file, delete these rows, and save 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 traverses forward from the last row, finds the row starting with '#', deletes it, and finally saves the file. The problem with this script is that if the file has not been saved, A File Save dialog box will pop up.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.