Modifying existing language settings
-
Hello!
I would like to add some custom tags in Notepad++'s HTML syntax highlighting, so that both the tag and it’s attributes are colored accordingly.
Is it possible to copy and modify OR import and edit the HTML language file from somewhere or do I have to do everything by hand (custom + official HTML tags) in the User-Defined Language window? -
The HTML syntax highlighter doesn’t have any user-defined keyword slots in the Style Configurator > HTML settings. Unfortunately, that means you can’t add them to the builtin lexer.
So you could use UDL (though it won’t have all the features of the HTML lexer), or you can add extra highlighting to a builtin lexer using regexes via the script
EnhanceAnyLexer.py
that @Ekopalypse shares in his github repo -
That’s a bummer. Oh well, thank you for your answer!
-
Actually, I just thought of and confirmed an alternative.
The
langs.xml
config file (so probably in%AppData%\Notepad++\langs.xml
if you have a normal installation) does list the standard tags that the HTML parser recognizes. If you follow best practices for editing config files, you can add a tag to the list, save, and then re-start Notepad++. For example, here’s a screenshot after I told Notepad++ that<aardvark>
was a valid HTML tag, but not<anteater>
:Note that
<aardvark>
and its attributes are colored just like normal tags, but<anteater>
is still in default color because it’s not a recognized tag name. -
@PeterJones This worked just the way I wanted. Thanks!
-
For future readers:
Note from the future: In 2024, the HTML TAG style does have a box for user-defined keyword slots. I don’t remember when it was added, but it’s been there for a while now (I can guarantee at least since v8.6.6, but I think a lot longer than that). So if you want this, start by upgrading to the most recent Notepad++.
Further, as I say here, I recently learned that the HTML lexer has the ability to enable “substyles” for the HTML TAG style, which you can assign your own color and your own list of keywords – so you could style your custom tags differently than the builtin tags if you want. Unfortunately, Notepad++ doesn’t (yet) enable those substyles, so you cannot do it natively in Notepad++. In this post, I share a script for the PythonScript which enables substyles – that specific post gives the example for PHP, but if you want to do the same for HTML, you would follow the same instruction, except edit the lists in
class HTML_SubstyleLexer
and append your colors/list definitions to theSCE_H_TAG
in that class’scolorize
.