Enable syntax highlighting in Nano for Java Server Pages (JSP)

0.00 avg. rating (0% score) - 0 votes

Recently when editing source files on an SSH remote session using nano, I discovered that JSP syntax highlighting is not supported by nano, not even in the latest version 6.2 on Ubuntu 22, simply because there is no nanorc file for JSP in /usr/share/nano. The set of improved nanorc files in this github repository, with support for many more languages, also does not contain JSP. I therefore decided to spend some time crafting my own nanorc file for JSP.

As far as language syntax goes, a JSP file is simply a set of HTML and CSS constructs combined with Javascript and Java source code, with some JSP-specific syntax like <% %> or <%= %>. As my purpose is simply to make JSP look more colorful and easier to read in nano, I simply combine the existing declarations found in html.nanorc, css.nanorc, java.nanorc and javascript.nanorc  into a new file named jsp.nanorc while merging/removing similar declarations, for example those for strings or comments. With this, I arrived at a new jsp.nanorc file which looks like the following:

Screenshot 2023-10-28 200228

Surprisingly, this appears to be good enough and JSP files are now properly highlighted, for the most part:

Screenshot 2023-10-28 200343

There are some limitations however. Because nanorc decides what to highlight based on regex matching without taking into account scopes, things like // this is an invalid comment will also be highlighted within CSS declaration (e.g. <style></style>). Constructs such as <!— HTML Comment —> will also be highlighted as comments within <% .. %>, even though this is not a valid comment in Java. I have also skipped highlighting for CSS classes or elements (e.g. #myButton or .myStyle) simply because the matching criteria is too simple (e.g. beginning with dot or hash) and will match other irrelevant parts of the codes and the JSP page will look like a mess. Improving this will require an understanding of the type of language that is currently being used within the JSP file, and apply, for example, only CSS highlighting within <style>…</style> or only Java highlighting within <% .. %>, something that is too complicated to achieve with nanorc.

On a side note, rudimentary autocomplete (suggesting all words that can be found in the current file) can be achieved by Ctrl-].  In the above example, typing r and press Ctrl-] will suggest red (from the CSS declarations) and then random (from the Java code). For it to work, you will have to type the first few characters before pressing Ctrl-], else nano will say “No word fragments” or “No matches”. Obviously I can’t expect Visual Studio-level of auto-completion, but a dropdown for me to select the words, populated from all files having the same extensions within the current folder, would have been immensely useful.  And don’t start me on the counter-intuitive keyboard shortcuts of vim or emacs, which is said to have much more sophisticated support for auto-completion.

The complete jsp.nanorc file can be downloaded here for those who are interested. Remove the .txt extension and copy to /usr/share/nano (or where the nanorc files are installed on your distribution) to enable syntax highlighting for JSP.

 

 

0.00 avg. rating (0% score) - 0 votes
ToughDev

ToughDev

A tough developer who likes to work on just about anything, from software development to electronics, and share his knowledge with the rest of the world.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>