Introduction
I think many people know
Notepad ++ , a handy free utility that acts as an “advanced” replacement for the standard Windows Notepad. As with any text editor, in Notepad ++ from time to time it becomes necessary to automate any repetitive actions that, because of the complexity of the logic, cannot be recorded as a macro. Fortunately, to solve this problem, there is no need to switch from
Notepad ++ to, for example, Word, in order to use the VB built into it.
Among plug-ins for
Notepad ++, there are extensions that implement the possibility of writing scripts for
Notepad ++ in different languages, such as JavaScript, Lua, PHP or Python. It was at the last that I decided to stop to solve my problem.
Formulation of the problem
Suppose we have the next task (taken from life).
1. In the selected text fragment (if nothing is selected, then in the whole document):
- number the '@' characters at the beginning of the line, replacing '@' with '@ 1', '@ 2', etc .;
- delete empty (including spaces and tabs) lines that go two or more in a row.
2. When starting the script, a request should be issued - from which number to start the numbering of the characters '@'. By default (by pressing "Enter"), the numbering should begin with 1. If you do not enter a numeric value, the query window should reappear until a number is entered.
')
3. If in the selected fragment of the text (or in the whole document - in case there is no selection) the '@' symbol is missing, the corresponding error message should be displayed.
4. Must be able to run the script:
- by clicking the corresponding button on the toolbar;
- using the keyboard shortcut;
- through the context menu of the right mouse button.
Decision
First we need to install a plugin for
Notepad ++ called
Python Script . With it, you can perform any operations with editable text, open / close files, switch tabs, execute menu commands
Notepad ++ , etc. - in a word, practically everything that can be done in
Notepad ++ .
Next, selecting
Notepad ++ Plugins-> Python Script-> New Script from the menu, create a script:
If we called the script “Empty Lines And Count”, then it can be launched from the menu
Plugins-> Python Script-> Scripts-> Empty Lines And Count . To add its button to the toolbar and make it possible to launch using the keyboard
shortcut , in the plugin settings (
Plugins-> Python Script-> Configuration ), select the script we created and add it to the menu and to the toolbar. Now after restarting
Notepad ++, the corresponding button will appear on the toolbar.
You can assign a shortcut to a script in the
Settings-> Shortcut mapper menu in the
Plugin commands section.
To add a script to the context menu of
Notepad ++ , add the following lines to the xml-file of settings (
Settings-> Edit Popup ContextMenu ) in the place you need (for example, before the first element):
<Item PluginEntryName="Python Script" PluginCommandItemName="Empty Lines And Count" ItemNameAs=" '@'"/> <Item id="0"/>

That's all. Now even routine actions with complex logic can be performed with one click.
Useful links: