2

I was wondering how could I make lines commented with a # highlighted in a different colour in nano?

I saw this question on askubuntu that shows how to syntax highlight for different languages. However this is overkill for just highlighting comments.

2

Syntax highlighting tends to be language specific. However, if you want to do it for all files, you can simply create a very very simple language definition. I took the Perl syntax style (which treats lines starting with # as comments) from /usr/share/nano/perl.nanorc and adapted it to:

syntax "All" "."
color green "^\s*#.*"

As far as I can tell, the nano syntax highlight format needs at least one test to define the file type, and then you can set filters for the color. So, I used the most simple test I can think of, that the file's name contains at least one character, and I named this syntax style All:

syntax "All" "."

I then told it to color lines starting with 0 or more spaces and then a # in green:

color green "^\s*#.*"

So, if you create a file called $HOME/.nanorc and paste those two lines into it, your comments will be highlighted in green.

  • When using .nanorc with includes to multiple language files, include this "All" first. That will make more specific configuration take precedence. – Tamir Daniely Apr 10 '18 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.