You are not logged in.
After years of using nano, I only recently learned that it supports syntax coloring... (Why would they turn that off by default? ) Well, I thought I'll make up for it by making extra good use of it from now on...
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:
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:
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.
/etc/rc.conf, /etc/hosts:
/etc/pacman.conf, /etc/group:
xorg.conf, some .desktop file:
httpd.conf (Apache config), php.ini:
,
More screenshots:
/etc/fonts/fonts.conf (uses XML)
/etc/inittab
/etc/fstab
/etc/inputrc
/etc/mime.types
/etc/protocols
/etc/xinetd.conf
---------------------
Update [2012-01-28]: Made some more improvements to the syntax definition (see post)
Last edited by sas (2012-02-01 15:26:43)
my AUR packages ~~ my community contributions
Offline
well done, I will give it a try.
Offline
Nice. I especially like what you did with /etc/group. Thank you for sharing this, fellow Arch user
Offline
Nice. I especially like what you did with /etc/group. Thank you for sharing this, fellow Arch user
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...
my AUR packages ~~ my community contributions
Offline
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
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
It works fine for me.
Thank you for this nice job.
Offline
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
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)
my AUR packages ~~ my community contributions
Offline
No problem, I was just curious to know where the problem was Now it's working as it should, thanks a ton!
Offline
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:
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.
my AUR packages ~~ my community contributions
Offline
(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
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... :-)
my AUR packages ~~ my community contributions
Offline
Wow. installed it couple of days ago and forgot about it and then I opened pacman.conf! What a difference! Thx alot!
Offline
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
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:
Last edited by sas (2012-02-01 15:18:52)
my AUR packages ~~ my community contributions
Offline
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
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
Very good! Thank you
Offline