Click here to Skip to main content
15,888,351 members
Articles / Programming Languages / Java

Enable Syntax Highlighting in Nano for Java Server Pages (JSP)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
31 Oct 2023CPOL3 min read 2.9K   1
Create custom nanorc file for JSP in nano to enhance syntax highlighting and readability, along with basic autocomplete
The article shows how to craft a nanorc file for JSP to enable syntax highlighting in nano, addressing limitations in regex-based highlighting and suggests rudimentary autocomplete options, aiming to improve the readability of JSP source code.

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 support 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 decided to 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 elements or classes (e.g., #myButton or .myStyle) simply because the simple matching criteria (e.g., beginning with hash or dot) will match other irrelevant parts of the codes and the resulting 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.

License

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


Written By
Technical Writer
Singapore Singapore
Since 2008, ToughDev has been publishing technical sharing articles on a wide range of topics from software development to electronics design. Our interests include, but are not limited to, Android/iOS programming, VoIP products, embedded design using Arduino/PIC microcontrollers, reverse-engineering, retro-computing, and many others. We also perform product reviews, both on new and vintage products, and share our findings with the community. In addition, our team also develops customized software/hardware solutions highly adapted to suit your needs. Contact us for more information.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA30-Oct-23 15:42
professionalȘtefan-Mihai MOGA30-Oct-23 15:42 

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.