You are not logged in.

#1 2012-01-15 14:51:40

sas
Member
Registered: 2009-11-24
Posts: 155

nano syntax highlighting: catch-all syntax for configuration files

After years of using nano, I only recently learned that it supports syntax coloring... (Why would they turn that off by default? hmm) Well, I thought I'll make up for it by making extra good use of it from now on... smile
Unfortunately it didn't ship a highlighting syntax for the the kind of files that I use nano the most for: system configuration files.
So I wrote my own, and after tweaking a bit here and there whenever I encountered a config file for which the highlighting wasn't satisfactory at first, I think the result is now good enough (screenshots below) that it's worth sharing with my fellow Arch users:

Code & Instructions:

Here is the syntax definition:

# config file highlighting

syntax "conf" "(\.(conf|config|cfg|cnf|rc|lst|list|defs|ini|desktop|mime|types|preset|cache|seat|service|htaccess)$|(^|/)(\w*crontab|mirrorlist|group|hosts|passwd|rpc|netconfig|shadow|fstab|inittab|inputrc|protocols|sudoers)$|conf.d/|.config/)"

# default text
color magenta "^.*$"
# special values
icolor brightblue "(^|\s|=)(default|true|false|on|off|yes|no)(\s|$)"
# keys
icolor cyan "^\s*(set\s+)?[A-Z0-9_\/\.\%\@+-]+\s*([:]|\>)"
# commands
color blue "^\s*set\s+\<"
# punctuation
color blue "[.]"
# numbers
color red "(^|\s|[[/:|<>(){}=,]|\])[-+]?[0-9](\.?[0-9])*%?($|\>)"
# keys
icolor cyan "^\s*(\$if )?([A-Z0-9_\/\.\%\@+-]|\s)+="
# punctuation
color blue "/"
color brightwhite "(\]|[()<>[{},;:=])"
color brightwhite "(^|\[|\{|\:)\s*-(\s|$)"
# section headings
icolor brightyellow "^\s*(\[([A-Z0-9_\.-]|\s)+\])+\s*$"
color brightcyan "^\s*((Sub)?Section\s*(=|\>)|End(Sub)?Section\s*$)"
color brightcyan "^\s*\$(end)?if(\s|$)"
# URLs
icolor green "\b(([A-Z]+://|www[.])[A-Z0-9/:#?&$=_\.\-]+)(\b|$| )"
# XML-like tags
icolor brightcyan "</?\w+((\s*\w+\s*=)?\s*("[^"]*"|'[^']*'|!?[A-Z0-9_:/]))*(\s*/)?>"
# strings
color yellow "\"(\\.|[^"])*\"" "'(\\.|[^'])*'"
# comments
color white "#.*$"
color blue "^\s*##.*$"
color white "^;.*$"
color white start="<!--" end="-->"

To install, save the above above code snippet as a file called conf.nanorc in the folder /usr/share/nano/ (or /usr/local/share/nano/ or similar if you feel strongly about the /usr <--> /usr/local separation), and then add the following to the end of the file /etc/nanorc:

## Configuration files (catch-all syntax)
include "/usr/share/nano/conf.nanorc"

Hints:

  • The colors I chose look good (imo) with the terminal background and color settings that I use, but might not look good, or even readable, with yours, so simply change the color names in the code snippet to whatever you prefer - valid color names are: consolecolors.png
    If you use a console with white background, you'll have to change at least the white color I chose for comments and punctuation.

  • The first code line in the snippet includes a regular expression that defines for which file names this syntax highlighting should be used. Whenever you encounter a config file that is not matched by this, but you would still like to open it with syntax highlighting, you can manually select this syntax with nano's -Y switch, like so:

    nano -Y conf myConfigFile

Technical Note:

  • It's implemented as a single catch-all syntax, since nano chooses which syntax to apply based on the filename, and in the case of config files usually not much can be learned about the content format from the file name extension (.conf can by anything from flat key/value tuples to XML, .ini can be the official INI format or something else, etc...).
    This means that some compromises have been made, so with this highlighting syntax probably no config file looks 100% as good as a highlighting syntax that would be specifically optimized for one kind of config format, but all in all the vast majority of config files should look pretty good.

Screenshots:

/etc/rc.conf/etc/hosts:
confrcconf.png confhosts.png

/etc/pacman.conf/etc/group:
confpacmanconf.png confgroup.png

xorg.conf,  some .desktop file:
confxorgconf.png confopenboxdesktop.png

httpd.conf (Apache config),  php.ini:
confhttpdconf.png, confphpini.png

More screenshots:

/etc/fonts/fonts.conf (uses XML)
/etc/inittab
/etc/fstab
/etc/inputrc
/etc/mime.types
/etc/protocols
/etc/xinetd.conf

See Also:

---------------------

Update [2012-01-28]: Made some more improvements to the syntax definition (see post)

Last edited by sas (2012-02-01 15:26:43)

Offline

#2 2012-01-15 17:36:13

Stemp
Member
From: Paris, Europe
Registered: 2011-04-26
Posts: 61
Website

Re: nano syntax highlighting: catch-all syntax for configuration files

Good job !


Sorry for my English - Home Page - «Violence never settles anything.» : Genghis Khan, 1162-1227

Offline

#3 2012-01-15 18:25:42

linux-ka
Member
From: ADL
Registered: 2010-05-07
Posts: 232

Re: nano syntax highlighting: catch-all syntax for configuration files

well done, I will give it a try.

Offline

#4 2012-01-22 00:42:04

tigrmesh
IRC Op
From: Florida, US
Registered: 2007-12-11
Posts: 794

Re: nano syntax highlighting: catch-all syntax for configuration files

Nice.  I especially like what you did with /etc/group.  Thank you for sharing this, fellow Arch user  smile

Offline

#5 2012-01-24 08:34:20

sas
Member
Registered: 2009-11-24
Posts: 155

Re: nano syntax highlighting: catch-all syntax for configuration files

tigrmesh wrote:

Nice.  I especially like what you did with /etc/group.  Thank you for sharing this, fellow Arch user  smile

You're welcome... :-)

About /etc/group, I didn't even intentionally optimize the syntax for that, I focused mostly on the various types of *.conf / *.cnf / *.cfg files (because those are the ones that really need a catch-all syntax, due to their generic filenames).
It just happened to also look OK with various "special" config files like /etc/group and /etc/rpc, so I activated it for them as well...

Offline

#6 2012-01-27 17:07:35

Shinryuu
Member
From: 俺のコスモス
Registered: 2010-02-27
Posts: 338

Re: nano syntax highlighting: catch-all syntax for configuration files

Pretty nice looking! For some reason I'm getting this error:

Error in /usr/share/nano/conf.nanorc on line 19: Bad regex "^\s*\[([A-Z0-9_-\.]|\s)+\]\s*$": Invalid range end

Offline

#7 2012-01-27 17:46:42

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: nano syntax highlighting: catch-all syntax for configuration files

Shinryuu wrote:

For some reason I'm getting this error:
<snip>

My guess is that the problem is with

"^\s*\[([A-Z0-9_-\.]|\s)+\]\s*$"
               ^^^^
             that bit

Maybe that 'underscore to period' is not a valid range in your locale.  Try running

sed 's/[_-\.]/Z/g'

on a text file.


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#8 2012-01-27 20:12:58

dimpard
Member
Registered: 2008-12-30
Posts: 10

Re: nano syntax highlighting: catch-all syntax for configuration files

It works fine for me.
Thank you for this nice job.

Offline

#9 2012-01-28 13:13:41

Shinryuu
Member
From: 俺のコスモス
Registered: 2010-02-27
Posts: 338

Re: nano syntax highlighting: catch-all syntax for configuration files

alphaniner wrote:
Shinryuu wrote:

For some reason I'm getting this error:
<snip>

My guess is that the problem is with

"^\s*\[([A-Z0-9_-\.]|\s)+\]\s*$"
               ^^^^
             that bit

Maybe that 'underscore to period' is not a valid range in your locale.  Try running

sed 's/[_-\.]/Z/g'

on a text file.

Mhh interesting... I'm using "en_US.UTF-8" here also I get this when running your code:
 

sed: -e expression #1, char 12: Invalid range end

Locale

LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE=C
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

Offline

#10 2012-01-28 13:34:06

sas
Member
Registered: 2009-11-24
Posts: 155

Re: nano syntax highlighting: catch-all syntax for configuration files

Shinryuu wrote:

Pretty nice looking! For some reason I'm getting this error:

Error in /usr/share/nano/conf.nanorc on line 19: Bad regex "^\s*\[([A-Z0-9_-\.]|\s)+\]\s*$": Invalid range end

Sorry, my bad...

To fix it, replace the following line...

icolor brightyellow "^\s*\[([A-Z0-9_-\.]|\s)+\]\s*$"

by this:

icolor brightyellow "^\s*\[([A-Z0-9_\.-]|\s)+\]\s*$"

(Note the hyphen being moved to the end of the [...]-group, so the regex parser doesn't think it signifies a character range...)

I've also made some other small improvements to my local copy of the syntax in the meantime, after I do some more testing with it I'll upload the new version.

Last edited by sas (2012-01-28 13:45:04)

Offline

#11 2012-01-28 13:46:41

Shinryuu
Member
From: 俺のコスモス
Registered: 2010-02-27
Posts: 338

Re: nano syntax highlighting: catch-all syntax for configuration files

No problem, I was just curious to know where the problem was smile Now it's working as it should, thanks a ton!

Offline

#12 2012-01-28 16:04:04

sas
Member
Registered: 2009-11-24
Posts: 155

Re: nano syntax highlighting: catch-all syntax for configuration files

UPDATE

OK, I updated the original post with a new version of the syntax definition.

No fundamental changes, but in some cases slightly improved matching of what should be considered a number / key / section-heading, and support for some more special syntax statements, benefiting mostly xorg.conf, inputrc, Apache config, and config files written by Qt/KDE applications... (Hopefully without breaking anything else.)

/etc/inputrc,   ~/.kde4/share/config/digikamrc:
confinputrc2.png confdigikamrc.png

If you find config files with aren't highlighted in an acceptable fashion, please post a comment and I'll see if it can be improved.

Offline

#13 2012-01-29 00:40:32

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: nano syntax highlighting: catch-all syntax for configuration files

sas wrote:

(Note the hyphen being moved to the end of the [...]-group, so the regex parser doesn't think it signifies a character range...)

The funny thing is, '_-\.' actually functioned as a valid range for me, catching most non-alphanum characters:

$ cat file ; sed 's/[_-\.]/ /g' file
`~!@#$%^&*()-=_+[]\{}|;':",./<>?END
`~  # %^&    = +     |       <> END

So I thought that might have been your intention.


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#14 2012-01-29 09:32:35

sas
Member
Registered: 2009-11-24
Posts: 155

Re: nano syntax highlighting: catch-all syntax for configuration files

alphaniner wrote:

The funny thing is, '_-\.' actually functioned as a valid range for me, catching most non-alphanum characters:

Interesting, I didn't realize it could work like that.

In my case nano didn't complain about the range either, except if it was called from another application, so it might indeed have to do with the LOCALE of the running shell.

In any case, I didn't intend it as a range, it was a bug in my regex... :-)

Offline

#15 2012-01-31 05:37:07

doug piston
Member
From: Seattle
Registered: 2011-09-11
Posts: 387
Website

Re: nano syntax highlighting: catch-all syntax for configuration files

I wuve this thread so much.

Offline

#16 2012-01-31 07:02:15

swanson
Member
From: Sweden
Registered: 2011-02-05
Posts: 759

Re: nano syntax highlighting: catch-all syntax for configuration files

Wow. installed it couple of days ago and forgot about it and then I opened pacman.conf! What a difference! Thx alot!

Offline

#17 2012-01-31 19:00:46

doug piston
Member
From: Seattle
Registered: 2011-09-11
Posts: 387
Website

Re: nano syntax highlighting: catch-all syntax for configuration files

I really like this idea, I deal with alot of .mk files and would love to see it there. Thanks for the work on this.

Offline

#18 2012-01-31 21:26:28

sas
Member
Registered: 2009-11-24
Posts: 155

Re: nano syntax highlighting: catch-all syntax for configuration files

doug piston wrote:

I deal with alot of .mk files and would love to see it there.

You mean GNU makefiles?

I'm afraid they might be out of scope for this generic config-file syntax.
Logically they're not system config files, and technically they're a pretty specialized and complex format (different "types" of rules, rules spanning multiple lines, rules containing arbitrary Bash code, etc.).

This is how an .mk file currently looks with this highlighting syntax:

$ nano -Y conf /usr/lib/httpd/build/rules.mk

rulesmk.png

And apart from highlighting variables of the form $$abc or $(abc), I'm not sure how much can be improved here without breaking the highlighting for more conventional config files.
It would probably be better to create a specialized highlighting syntax just for .mk files.

EDIT: I sat down and did just that, here's the result: nano syntax highlighting: GNU makefiles, and here is how the above makefile snipped looks with it:
rulesmk2.png

Last edited by sas (2012-02-01 15:18:52)

Offline

#19 2012-01-31 22:09:50

doug piston
Member
From: Seattle
Registered: 2011-09-11
Posts: 387
Website

Re: nano syntax highlighting: catch-all syntax for configuration files

Oh, it says right in the OP to use the -Y switch.
Reading > me. Thanks and appreciate the response.

Last edited by doug piston (2012-01-31 22:12:15)

Offline

#20 2012-03-27 04:00:20

akspecs
Member
Registered: 2012-02-11
Posts: 173

Re: nano syntax highlighting: catch-all syntax for configuration files

Hey man, thanks for this beautiful coloring you've given us!  I just put this into my /usr/share/nano and I'm totally loving it.  Awesome.

Offline

#21 2012-06-26 15:35:50

Ypnose
Member
From: Jailed in the shell
Registered: 2011-04-21
Posts: 353
Website

Re: nano syntax highlighting: catch-all syntax for configuration files

Nice work. You made my day. I'm not like vim lovers, I still like nano.


Github -- My terminal font Envypn

Offline

#22 2012-06-26 16:24:56

n1x4
Member
From: Chernobyl
Registered: 2012-02-17
Posts: 149
Website

Re: nano syntax highlighting: catch-all syntax for configuration files

EXCELLENT WORK!!!! Thank you.
As ypnose said, not a vim lover... nano FTW!


||github||

Offline

#23 2012-07-09 19:39:23

vasyabelkin
Member
Registered: 2009-09-10
Posts: 19

Re: nano syntax highlighting: catch-all syntax for configuration files

Very good! Thank you

Offline

#24 2012-07-09 20:01:29

ecmel
Member
Registered: 2012-02-13
Posts: 29
Website

Re: nano syntax highlighting: catch-all syntax for configuration files

Very nice work, thanks a lot. I was looking for this.

smile

Offline

Board footer

Powered by FluxBB