Configure Nano Syntax Highlighting on Synology

By Jimmy Bonney | June 15, 2013

Highlighters

Nano is a small text editor available from the console. It is an alternative to editors like vi or pico but comes packed with features making it more user-friendly – especially for beginners. Nano is available from pretty much all Linux distributions. In this article, we’ll focus on installing and configuring its syntax highlighting feature on a Synology NAS.

Install nano

In order to install nano, ipkg needs to be installed. If it isn’t, have a look at this previous article or go directly to the Synology forum.

Once ipkg is installed, simply run the following commands as root:

1
2
ipkg update
ipkg install nano

Now, nano is simply available from the command line.

Nano with syntax highlighting

Configure Syntax Highlighting

The default configuration of nano on the Synology drive is not to highlight code in the editor. Fixing this is quite easy. Start by editing the nanorc file:

1
nano /opt/etc/nanorc

Navigate to the bottom of the file, and uncomment the entries related to the languages for which you would like to enable syntax highlighting. In my case, I uncommented all of them so that it looks like the following.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
## Nanorc files
include "/opt/share/nano/nanorc.nanorc"

## C/C++
include "/opt/share/nano/c.nanorc"

## Makefiles
include "/opt/share/nano/makefile.nanorc"

## Cascading Style Sheets
include "/opt/share/nano/css.nanorc"

## Debian files
include "/opt/share/nano/debian.nanorc"

## Gentoo files
include "/opt/share/nano/gentoo.nanorc"

## HTML
include "/opt/share/nano/html.nanorc"

## PHP
include "/opt/share/nano/php.nanorc"

## TCL
include "/opt/share/nano/tcl.nanorc"

## TeX
include "/opt/share/nano/tex.nanorc"

## Quoted emails (under e.g. mutt)
include "/opt/share/nano/mutt.nanorc"

## Patch files
include "/opt/share/nano/patch.nanorc"

## Manpages
include "/opt/share/nano/man.nanorc"

## Groff
include "/opt/share/nano/groff.nanorc"

## Perl
include "/opt/share/nano/perl.nanorc"

## Python
include "/opt/share/nano/python.nanorc"

## Ruby
include "/opt/share/nano/ruby.nanorc"

## Java
include "/opt/share/nano/java.nanorc"

## Fortran
include "/opt/share/nano/fortran.nanorc"

## Objective-C
include "/opt/share/nano/objc.nanorc"

## OCaml
include "/opt/share/nano/ocaml.nanorc"

## AWK
include "/opt/share/nano/awk.nanorc"

## Assembler
include "/opt/share/nano/asm.nanorc"

## Bourne shell scripts
include "/opt/share/nano/sh.nanorc"

## POV-Ray
include "/opt/share/nano/pov.nanorc"

## XML-type files
include "/opt/share/nano/xml.nanorc"

Save the file (ctrl + O) and quit the editor (ctrl + X). Now, we’re almost there but there is however one slight issue left.

Fix bad regex error message

If you try to start nano again, you’ll see a long list of error messages complaining about bad regex such as:

1
2
3
4
5
6
7
Error in /opt/share/nano/sh.nanorc on line 6: Bad regex "[[:<:]](case|do|done|elif|else|esac|exit|fi|for|function|if|in|local|read|return|select|shift|then|time|until|while)[[:>:]]": Invalid character class name

Error in /opt/share/nano/sh.nanorc on line 8: Bad regex "-[Ldefgruwx][[:>:]]": Invalid character class name

Error in /opt/share/nano/sh.nanorc on line 9: Bad regex "-(eq|ne|gt|lt|ge|le|s|n|z)[[:>:]]": Invalid character class name

Error in /opt/share/nano/sh.nanorc on line 10: Bad regex "[[:<:]](cat|cd|chmod|chown|cp|echo|env|export|grep|install|let|ln|make|mkdir|mv|rm|sed|set|tar|touch|umask|unset)[[:>:]]": Invalid character class name

Others have already solved the problem and the solution consist in replacing all \< and \> by \b. This can be done manually (in this case, the replace command of nano is ctrl+\) or we can run a little script that will take care of this for us.

Simply execute the following from any folder (this needs to be done as root):

1
nano fix_bad_regex.sh

And in the editor, paste the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
NEW="\\\b"
DPATH="/opt/share/nano/*.nanorc"
BPATH="/opt/share/nano/"
[ ! -d $BPATH ] && mkdir -p $BPATH || :
for f in $DPATH
do
  if [ -f $f -a -r $f ]; then
    /bin/cp $f "$f.bak"
    sed -i "s/\\\</$NEW/g" "$f"
    sed -i "s/\\\>/$NEW/g" "$f"
   else
    echo "Error: Cannot read $f"
  fi
done

This script simply go through all files with a nanorc extension that are located in the /opt/share/nano/ folder and replaces the characters \< and \< by \b. Before replacing anything, it makes a backup of the files should you want to go back to the initial nanorc files.

Make the file executable:

1
chmod +x fix_bad_regex.sh

And execute it:

1
sh fix_bad_regex.sh

From now on, nano will stop complaining about the bad regex and syntax highlighting will be available for all files that you edit with nano.



comments powered by Disqus