Click here to Skip to main content
14,359,408 members

Tools: Notepad++ – Syntax Highlighting

Rate this:
4.67 (3 votes)
Please Sign up or sign in to vote.
4.67 (3 votes)
29 Dec 2013CPOL
This post is about the syntax highlighting in the tool and how you can customize it.

I’ve posted about Notepad++ (Npp) tool briefly in a different context. I’ve been using it for a while and the more I use, the more I discover about the tool. This post is about the syntax highlighting in the tool and how you can customize it.

Npp is simply a text editor. It does the same thing Windows’ Notepad does and then some. Hence the name Notepad++. With it, you can open almost any text file. Have you ever tried to open a text file from Unix (or Linux) in Windows’ Notepad? All lines will be collapsed into a big jumble? This won’t happen in Notepad++. It also allows you to adapt various encoding in file (ASCII/ANSI, UNICODE etc) and thus you won’t see box characters (well, most of the time). So, it’s a great text editor. But that’s not all. What makes it a great tool are it’s easy-to-use user interface, it’s search capabilities (includes Regex out of the box), plugins etc. If you haven’t already tried it, download it from here.

As developers we tend to expect a lot more from any tool. I myself have used several editors, starting DOS’s EDIT, Unix Vi etc. (I still remember Brief Editor of the early 90′s made by a company called Underware!) Each had unique capabilities and weaknesses. One thing they were all missing was the Syntax highlighting. For a long time, this was the feature of IDEs/Editors that came with the language. But in the era of extensible editors like Eclipse, we’ve come to expect Syntax highlighting as a minimum for any text editor.

I have always wondered how the programming tools color their syntax. I can understand a dedicated tool like PowerBuilder. They know their language, so they can just hard code it in their programs. How does a simple editor like Notepad++ do that without having knowledge about each language it supports? These tools provide some kind of mechanism (proprietary or standard) to define various aspects of the syntax and how to highlight them. But, they just don’t know anything about the language itself. That is the syntax highlighting in such tools is based on pure syntax and not the semantics of the language. This is the key to understanding some anomalies in highlighting in these tools.

Notepad++ (Npp) is one of the editors based around a standard editing component called Scintilla. According to their home page, “Scintilla is a free source code editing component. It comes with complete source code and a license that permits use in any free project or commercial product.”. More on this later. Npp comes with syntax definitions for several languages. But, if your language of choice is not in the list, no worries; you can simply add another “User Defined Language” (UDL). With the latest version (6.2.x), it’s improved and called UDL2.

It’s the UDL that I am going to talk about in this post. If you open Npp and click on Language menu, you will see a list of languages available. You can then click “Define Your language” option and get going with adding the definition for your language. I used this define the syntax highlighting for PowerBuilder code.

User Defined Language (UDL)

Image 1

Fig 1. Npp UDL dialog

Using this dialog, you can create new syntax definitions, import, export and copy from another language definition. There are several tabs here. We will come to Folder & Default in a minute. Keywords Lists is where you put in the keywords in your language. You can separate the keywords in your language into several groups. For e.g., in my example below, I’ve 2 lists – one for all the reserved words in PowerBuilder and the second list contains all the types. Separating it thus allows you style your keywords distinctly.

Image 2

Fig 2. Setting up Keyword Lists

When you define an entity – here Keyword list, you can also attach a styler to it. The styler is where you define the fonts and colors for the text for the particular type of entity. For e.g., I’ve colored PB reserved words in blue as shown in Fig3.

Image 3

Fig 3. Styler Dialog

Next Tab in the UDL dialog is the “Comment & Number”. This is where you can define the commenting symbols in your language. For comments they have 2 styles. Line Style which is a single comments. In PB (C, C++, Java etc) single line comments are identified // at the beginning of the text. Such comments can be at the beginning or end of the line or by themselves.

The second style is C style languages is the multi-line comments, anything in between /* and */. Here is a screenshot of the comment section. As you can see, there is a lot more you can do with it. In the styler for the comments, you can also specify about nested comments.

Image 4

Fig 4. Setting up Syntax highlighting for comments

Similarly, you can highlight numbers.

The last tab is the Operators & Delimiters. This is where you list out the operators and any delimiters in the language you are defining. Operators are the symbols like +, -, /, * etc. Comma (,), semi-colon(;), colon(:), pipe (|) are examples of delimiters. Again, use the stylers to format them.

I will now get back the first tab – Folders & Default. Default is simple – This the normal text that is not defined in any of the tabs above – i.e., text that is not a keyword, comment, operator or a number. To define this, just click on the Default Style-> Styler button.

The folders section is a bit more interesting. Have you ever seen code folding? Eclipse has it. I believe, Visual Studio does. PowerBuilder itself does not offer it. It’s the feature in some editors that let you selectively hide sections of code, so you can see the “big picture”. For e.g., if you are working with a multi-level nested IF statements, you can hide inner ones to see what the outer most if/else does. You typically see code folding available for any control structures (let’s call it block definers) in the programming language – like IF, CASE, FOR, DO, {, } etc. These typically have an opening and closing marker text or symbol. The code folding also helps in such cases, to see if any block is missing closures.

In this tab, Npp UDL offers 2 styles – one that doesn’t require separators and the other that does. This is a bit confusing and I had to try both to understand. The one that doesn’t require separator are symbols like {, }, (, ) etc. Even if you have text touching them, they are identifiable. You put them in the first style. The second style, that needs separators (typically space), will contain all the other block definers. For e.g., in PB we have IF…ELSE…END IF to define a IF block. The opener is the IF, END IF is the closer. ELSE happens to be the middle (Earlier versions of UDL only supported open and close. So, upgrade to the latest version of Npp, if you haven’t already done so). Notice END IF is actually 2 words; in such cases surround them in Double quotes.

Folding in comments allows you to put specific comments in your code, that you can use for folding the section. We often comment out whole block of codes to test portions of code. Just add marker texts (open, middle, close) defined earlier to such comments, you will be able to just hide the whole block.

Image 5

Fig 5. Code Folding in Npp

Of course, you can add styles in the stylers.

Here is a sample PB code in Npp, after I created the UDL “Powerbuilder” and applied to the file opened.

Image 6

Fig 6. Editing PB code in Npp

Note: When I added code folding the syntax highlighting for the keywords in Code folding (IF, END IF, CHOOSE CASE etc) seem to disappear. It may be a bug in the current version of Npp. I will check and post back here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sam Varadarajan
Software Developer (Senior) City of Los Angeles
United States United States
Originally a Physics major, fell in love with Microprocessors and switched to Computer Science 20+ years ago. Since then, dabbled in various languages including, PowerBuilder, Oracle, Java, C, C++, Perl, Python etc. Constantly striving for quality and performance too.

I try to help fellow developers with technology as a way of "giving back to the community". Blogging became a natural extension of that effort. Still learning to perfect that art. If one new programmer out there benefits from this blog, my time and effort are fully worth it.

The underlying theme in my blogs is power and beauty of programming (and technology in general). A well written program gives me the sense of awe you get when you look at a man made wonder like Angkor Wat. You experience poetry, art, mystique, power all at once. A program and the troubleshooting that ensues also gives you a feeling you get while reading a mystery novel!

Comments and Discussions

QuestionHighlighting issues in Notepad++ Pin
Pihom16-Dec-14 8:51
memberPihom16-Dec-14 8:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Technical Blog
Posted 29 Dec 2013

Tagged as


4 bookmarked