Configuring nano text editor in Ubuntu (nanorc, syntax highlighting)

I almost exclusively use nano for my text editing needs but unfortunately it doesn’t have great options enabled by default and it has limited syntax highlighting for writing any code.

This can be fixed fairly easily. Nano has a config file located at /etc/nanorc  which can be edited to enable changes for all users. To limit changes to one user save the config file as ~/.nanorc

First run

to check that your version of nano has been compiled to allow it to use the nanorc config file. You should see ‘–enable-nanorc’ in the output if it has been compiled correctly…

I would suggest making a backup of the nanorc file first in case you want to revert to the default if you screw it up somehow…

cp /etc/nanorc /etc/nanorc.old

Then open nano rc in nano (obviously)

You can see all the nanorc options here and the /etc/nanorc  file is very well commented so it’s not hard to understand what changes you want to make. These are the nanorc lines which I’ve changed/enabled…

  • set smooth
    • This will scroll the nano screen by one line at a time when you reach the bottom of the screen instead of the big jumps it usually takes.
  • set autoindent
    • This will keep your tab indent when moving to a next line in nano.
  • set tabstospaces
    • I prefer my tabs to be spaces. Own preference though.
  • set mouse
    • Allows use of the mouse to move around.
  • set backup
    • This tells nano to save old copies of files to the directory below in case you need a previous version.
  • set backupdir "/home/<user>/nanobackups"
    • Remember to create this directory too. And change to your username or a different location of your choice.
  • set morespace
    • Removes the blank line below the title bar.
  • set nohelp
    • Removes the ‘help’ section at the bottom of the nano interface.
  • set tabsize 2
    • I prefer a tab size of 2 spaces. I know some prefer 4 though.

I also wanted to add syntax highlighting. There are usually some default syntax highlighting rules installed with nano, you can look in /usr/share/nano  to see it these are there. There will be one file for each set of rules. However some very helpful folks online have written or pulled together some better highlighting rules. The two best sets of rules I’ve found at:

https://github.com/tech4david/nano-highlight

https://github.com/nanorc/nanorc

I downloaded these and saved in subdirectories of the default nanorc syntax highlighting directory…

The files will now be saved in  /usr/share/nano/tech4david/nano-highlight-master  & /usr/share/nano/nanorc/nanorc-master  respectively. We need to run a couple of commands to install them…

The last line of the output of this command will tell you where it has installed the files. Let’s move them to our subdirs of /usr/share/ and clean up after ourselves. I’ll use my own (/root/.nano/syntax/) as an example…

Then do the same installation steps and moving of syntax files for the second set of rules you downloaded.

You can then edit your nanorc file to tell it to use one of these as a source for it’s syntax highlighting. I’d suggest trying both and seeing which you like better. You can also mix and match if you like some rules from one and some from another. You could even amend the rulesets and write your own.

To enable syntax highlighting and include all the rules at once you can comment out the existing lines in nanorc – there are about 20 of them at the end of the file which look like this: include "/usr/share/nano/nanorc.nanorc"

We need to include all our new syntax rules in nanorc so run these commands to do add the necessary lines to the end of nanorc…

or

Useful help for configuring this file found here:

1 Comment

Leave a Reply

(email optional)