How to enable syntax highlighting for INI files in GNU Nano

How to enable syntax highlighting for INI files in GNU Nano

GNU nano is a simple terminal-based text editor. Though not as powerful as Emacs or Vim, it is easy to learn and use. A lot of developers prefer this editor as it's very simple to use and pretty useful when you only want to edit a single file quickly on your server.

One of those files that you need to change often in this kind of editor are configuration file, like ini files. Nano offers syntax highlighting for many file types, however not for ini files. If you want to highlight this kind of files as well, you will need to follow an extra step. In this article, we'll show you how to highlight INI files on nano in Ubuntu.

1. List available Nano Syntax Highlight Files

As first step, discover which languages are available in nano to highlight its syntax with the following command:

ls /usr/share/nano/

This will list all the nano syntax highlighting files in the given directory:

root@server:~$ ls /usr/share/nano/
asm.nanorc     fortran.nanorc   man.nanorc     ocaml.nanorc   ruby.nanorc
awk.nanorc     gentoo.nanorc    mgp.nanorc     patch.nanorc   sh.nanorc
c.nanorc       groff.nanorc     mutt.nanorc    perl.nanorc    tcl.nanorc
cmake.nanorc   html.nanorc      nano-menu.xpm  php.nanorc     tex.nanorc
css.nanorc     java.nanorc      nanorc.nanorc  pov.nanorc     xml.nanorc
debian.nanorc  makefile.nanorc  objc.nanorc    python.nanorc

If you don't find the ini.nanorc file, then you can install it with the next step.

2. Create INI Nano Syntax Highlighting File

In order to provide syntax highlighting to your file, if the default file doesn't exist, you need to create the syntax highlighting file for this language. This file is the ini.nanorc file and you need to create it in the mentioned directory. Run nano to create the file:

sudo nano /usr/share/nano/ini.nanorc

and paste the following content:

syntax "ini" "\.ini(\.old|\.bak|\.example|~)?$"
 
## Values
color brightred "=.*$"

## Equal sign
color green "="

## Numbers
color brightblue "-?[0-9\.]+\s*($|;)"

## ON/OFF
color brightmagenta "(ON|OFF|On|Off|on|off)\s*($|;)"

## Sections
color brightcyan "^\s*\[.*\]"

## Keys
color cyan "^\s*[a-zA-Z0-9_\.]+"

## Comments
color brightyellow ";.*$"

Visit the official repository of Nano Highlight, a spiffy collection of nano syntax highlighting files for more information and languages available for nano. This file will be automatically added into nano and will highlight ini files. Save changes and proceed with the last step.

3. Create Test INI File to see results

As final step, you need to test wheter the highlight works or not. Proceed to create a test file with nano and write some INI based content on it, for example:

# A comment 
; This is a comment too 
 
[SectionOne]
 
key = "value"
integer = 1234
real = 3.14
string1 = "Case 1"
string2 = 'Case 2'
multivalue[] = "first"   # in-line comments 
multivalue[] = 'second'  # are supported as well 
 
; Section: SectionTwo
[SectionTwo]
 
key = new value
integer = 1234
real = 3.14
string1 = Case 1
string2 = Case 2
string3 = Case 3
multivalue[] = first
multivalue[] = second
multivalue[] = third

Save the file, edit it again and you will now see the INI syntax highlighted.

Happy coding !

Become a more social person