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:
Surprisingly, this appears to be good enough and JSP files are now properly highlighted, for the most part:
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.