Thursday, 9 April 2009

How-To: Enable Syntax Highlighting in Nano

Nano is a popular, user-friendly text editor which can be ran in a shell, without the need of an X Server. It can be very useful especially for editing text files (like configuration files or scripts) in a fast manner, being faster than more advanced, graphical text editors.

Nano has syntax highlighting support for programming languages, which is disabled by default. However, Nano provides default rules for several languages like Perl, Python, or C, to name a few. These highlighting definitions are kept inside the /usr/share/nano/ directory, and for each language there is available a file with rules.

For example, ls /usr/share/nano will output the following on my system, which has Nano 2.0.7 installed:

$ ls /usr/share/nano/
asm.nanorc groff.nanorc java.nanorc mutt.nanorc nanorc.nanorc perl.nanorc python.nanorc sh.nanorc
c.nanorc html.nanorc man.nanorc nano-menu.xpm patch.nanorc pov.nanorc ruby.nanorc tex.nanorc

These are default syntax highlighting rules for several languages, like Java, Perl, Python, Bash, C, HTML or Ruby.

In order to enable syntax highlighting in Nano based on one of these files, edit the ~/.nanorc file, where ~ is your home directory. Notice that you will have to create this file if it doesn't already exist. Then add lines to it in this fashion:

include "/usr/share/nano/c.nanorc"
include "/usr/share/nano/perl.nanorc"
include "/usr/share/nano/sh.nanorc"
include "/usr/share/nano/html.nanorc"

And so on for each language you want to have syntax highlighting enabled. Those four lines will include the files c.nanorc, perl.nanorc, sh.nanorc and html.nanorc, which will define highlighting rules for C, Perl, Bash and HTML respectively. This can also be accomplished by copying the contents of one of those files directly into ~/.nanorc. For example:

cat /usr/share/nano/c.nanorc >> ~/.nanorc

The above command will append the contents of /usr/share/nano/c.nanorc at the end of ~/.nanorc, enabling syntax highlighting for C. You can also search on Google for other rules, if the default colours don't fit well.

Syntax highlighting for C in Nano

Notice: This tutorial is a later, revised version of the tutorial which I initially published here.


TĂșlio Ornelas said...

Very nice! thanks!

Anonymous said...

Putting each include file inside "" isn't necessary, and on some systems, causes nano to dump error messages when it tries to find "/path/to/somefile" as opposed to /path/to/somefile.

Alan MacGregor said...

Thanks very much for this, been using nano for several years to program in python and never knew about this.

Nathaniel Kofalt said...

Hey! Thanks for the guide. For those interested, here is a way to do it pain-free from the terminal:

for i in `echo /usr/share/nano/*.nanorc` ; do echo include \"$i\" >> ~/.nanorc ; done; echo "Done"

Use that exactly; it simply adds every nanorc file found into your preferences folder with the desired syntax. If you have issues with quotes (the anon above me mentioned some), just change the


to be


I hope this helps!

distrohopper said...

You can also copy /etc/nanorc to ~/.nanorc and make many other custom changes to nano's behavior. It's quite configurable (I never knew there was so many options!).

Alex said...

Thanks, this is just what I was looking for.

I wonder why these aren't enabled by default. Who, in the world of today, would want to use a text editor without syntax highlighting?

Anonymous said...

Thank you so much. This was very helpful.

Anonymous said...

I got rid of the quotes and it worked great!