python script for HTML tags in Notepad++

if you have PythonScript plugin, you could do something like I’ve done for automating markdown: make a script for each type of tag that you’d like to insert around highlighted text, and then use the right-click context menu, or keyboard shortcuts, to insert those.

For the scripts, they all look like:

for i in range(editor.getSelections()):
    start = editor.getSelectionNStart(i)
    end = editor.getSelectionNEnd(i)
    word = editor.getTextRange(start,end)
    editor.setTarget(start, end)

Which is putting the ** before and after the highlighted text. So for you, you would just use


… to make the highlighted text bold. If nothing is highlighted, it will just put in the empty <b></b> tag pair.

If I were doing this for you, I would create a subdirectory of %AppData%\plugins\config\PythonScript\ called HtmlActions or something, then create a separate script (as above) for each of the tags (bold, italic, underline, img, link, …), and edit all of those scripts like above.

To make them available for keyboard shortcuts or RClick context menu, you would first need to go to Plugins > PythonScript > Configuration … and add them to the “Menu Items” list, hit OK, and restart NPP. To make them available for Toolbar icons, ADD them to the “Toolbar Icons” list in the same dialog.

To add to context menu, use something similar to what I have in %AppData%\Notepad++\contextMenu.xml:

    <Item FolderName="Markdown commands" PluginEntryName="Python Script" PluginCommandItemName="MarkBold"           ItemNameAs="Mark Selection as Bold"           />
    <Item FolderName="Markdown commands" PluginEntryName="Python Script" PluginCommandItemName="MarkEm"             ItemNameAs="Mark Selection as Emphasis"       />
    <Item FolderName="Markdown commands" PluginEntryName="Python Script" PluginCommandItemName="MarkCodeInline"     ItemNameAs="Mark Selection as Inline-Code"    />
    <Item FolderName="Markdown commands" PluginEntryName="Python Script" PluginCommandItemName="MarkKbd"            ItemNameAs="Mark Selection as Keyboard"       />
    <Item FolderName="Markdown commands" PluginEntryName="Python Script" PluginCommandItemName="MarkLinkInlineURL"  ItemNameAs="Mark Selection as URL of Inline-Link"   />
    <Item FolderName="Markdown commands" PluginEntryName="Python Script" PluginCommandItemName="MarkLinkInlineText" ItemNameAs="Mark Selection as Text of Inline-Link"  />
    <Item FolderName="Markdown commands" id="0"/><!-- divider INSIDE submenu -->
    <Item FolderName="Markdown commands" PluginEntryName="Python Script" PluginCommandItemName="Markdown"           ItemNameAs="Convert Markdown to HTML"         />

To add a shortcut command, go to Settings > Shortcut Mapper > Plugin Commands; if you’ve got a new enough NPP version, you can type “python” in the Filter box, otherwise, just scroll down to the “PythonScript” section; select the script you want to set the shortcut for, and Modify.

To add the icon, all you need to do is ADD it to the list, above. You can also set a different icon for each script; see the PythonScript documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *