Tip: Easily Enable Syntax Highlighting in Nano Editor

Posted by Chris7mas on Sep 23, 2008 11:00 PM EDT
TuxArea; By Craciun Dan
Mail this story
Print this story

For those who prefer to edit C, Bash, PHP or other source files using Nano instead of an advanced IDE like Emacs or Vim, enabling syntax highlighting is probably a must-have. To enable highlighting in Nano for various programming languages, you will first need to edit (or create if it doesn't exist) the file ~/.nanorc, where ~ is your home directory and put in it lines like this:

Nano is one of the most lightweight and user-friendly text editors for command-line. For those who prefer to edit C, Bash, PHP or other source files using Nano instead of an advanced IDE like Emacs or Vim, enabling syntax highlighting is probably a must-have. To enable highlighting in Nano for various programming languages, you will first need to edit (or create if it doesn't exist) the file ~/.nanorc, where ~ is your home directory and put in it lines like this:

include "/usr/share/nano/c.nanorc"
include "/usr/share/nano/php.nanorc"
include "/usr/share/nano/sh.nanorc"

These three lines include highlighting rules for C (c.nanorc), PHP (php.nanorc) and Bash (sh.nanorc). In order to see all the available files with syntax highlighting rules, use:

ls /usr/share/nano/

Which outputs the following on my Kubuntu 8.04 box with Nano 2.0.7:

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

These are default highlighting rules which come with Nano. However, you can always search for other rules on Google and include those instead of the default ones, depending on which terminal background you use etc.

Here is an example on how default C highlighting works:





The ~/.nanorc file allows you to configure Nano in many ways and change its default behaviour. When Nano starts, it reads configuration options from /etc/nanorc, and then individually for each user from the ~/.nanorc file, if it exists. To see what kind of options you can set/unset in this file, use:

man nanorc

Nano homepage

Full Story

  Nav
» Read more about: Story Type: Tutorial

« Return to the newswire homepage

This topic does not have any threads posted yet!

You cannot post until you login.