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

nano -V

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…

GNU nano version 2.2.6 (compiled 14:11:11, Oct 1 2012)
(C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
2008, 2009 Free Software Foundation, Inc.
E-mail: nano@nano-editor.org Web: http://www.nano-editor.org/
Compiled options: --disable-wrapping-as-root --enable-color --enable-extra --enable-multibuffer --enable-nanorc --enable-utf8

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)

sudo nano /etc/nanorc

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…

cd /usr/share/nano/
wget https://github.com/tech4david/nano-highlight/archive/master.zip
unzip master.zip -d /usr/share/nano/tech4david
wget https://github.com/nanorc/nanorc/archive/master.zip
unzip master.zip -d /usr/share/nano/nanorc
rm master.zip

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…

cd /usr/share/nano/tech4david/nano-highlight-master
make install

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…

mv /root/.nano/syntax/*.nanorc /usr/share/nano/tech4david/
rm -rf /root/.nano/syntax
rm -rf /usr/share/nano/tech4david/nano-highlight-master

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…

echo -e "\n## Include new syntax highlighting rules" >> /etc/nanorc
echo "include /usr/share/nano/tech4david/ALL.nanorc" >> /etc/nanorc

or

echo -e "\n## Include new syntax highlighting rules" >> /etc/nanorc
echo "include /usr/share/nano/nanorc/ALL.nanorc" >> /etc/nanorc

Useful help for configuring this file found here:

1 Comment

Leave a Reply

(email optional)


Warning: Undefined array key "rerror" in /home/public/blog/wp-content/plugins/wp-recaptcha/recaptcha.php on line 291