nano: Syntax Highlighting
7.2 Syntax Highlighting
=======================
Coloring the different syntactic elements of a file is done via regular
expressions (see the ‘color’ command below). This is inherently
imperfect, because regular expressions are not powerful enough to fully
parse a file. Nevertheless, regular expressions can do a lot and are
easy to make, so they are a good fit for a small editor like ‘nano’.
A separate syntax can be defined for each kind of file via the
following commands in a nanorc file:
‘syntax "NAME" ["FILEREGEX" ...]’
Start the definition of a syntax with this NAME. All subsequent
‘color’ and other such commands will be added to this syntax, until
a new ‘syntax’ command is encountered.
When ‘nano’ is run, this syntax will be automatically activated if
the current filename matches the extended regular expression
FILEREGEX. Or the syntax can be explicitly activated by using the
‘-Y’ or ‘--syntax’ command-line option followed by the NAME.
The ‘default’ syntax is special: it takes no FILEREGEX, and applies
to files that don’t match any syntax’s FILEREGEX. The ‘none’
syntax is reserved; specifying it on the command line is the same
as not having a syntax at all.
‘header "REGEX" ...’
If from all defined syntaxes no FILEREGEX matched, then compare
this REGEX (or regexes) against the first line of the current file,
to determine whether this syntax should be used for it.
‘magic "REGEX" ...’
If no FILEREGEX matched and no ‘header’ regex matched either, then
compare this REGEX (or regexes) against the result of querying the
‘magic’ database about the current file, to determine whether this
syntax should be used for it. (This functionality only works when
‘libmagic’ is installed on the system and will be silently ignored
otherwise.)
‘linter PROGRAM [ARG ...]’
Use the given PROGRAM to do a syntax check on the current file.
(This overrides the speller function.)
‘formatter PROGRAM [ARG ...]’
Use the given PROGRAM to automatically reformat text — useful for a
programming language like Go. (This overrides the speller and
linter functions.)
‘comment "STRING"’
Use the given string for commenting and uncommenting lines. If the
string contains a vertical bar or pipe character (|), this
designates bracket-style comments; for example, "/*|*/" for CSS
files. The characters before the pipe are prepended to the line
and the characters after the pipe are appended at the end of the
line. If no pipe character is present, the full string is
prepended; for example, "#" for Python files. If empty double
quotes are specified, the comment/uncomment functions are disabled;
for example, "" for JSON. The default value is "#".
‘color FGCOLOR,BGCOLOR "REGEX" ...’
Display all pieces of text that match the extended regular
expression "regex" with foreground color "fgcolor" and background
color "bgcolor", at least one of which must be specified. Valid
colors for foreground and background are: white, black, red, blue,
green, yellow, magenta, and cyan. You may use the prefix "bright"
to get a stronger color highlight for the foreground. If your
terminal supports transparency, not specifying a "bgcolor" tells
‘nano’ to attempt to use a transparent background.
‘icolor FGCOLOR,BGCOLOR "REGEX" ...’
Same as above, except that the matching is case insensitive.
‘color FGCOLOR,BGCOLOR start="FROMRX" end="TORX"’
Display all pieces of text whose start matches extended regular
expression "fromrx" and whose end matches extended regular
expression "torx" with foreground color "fgcolor" and background
color "bgcolor", at least one of which must be specified. This
means that, after an initial instance of "fromrx", all text until
the first instance of "torx" will be colored. This allows syntax
highlighting to span multiple lines.
‘icolor FGCOLOR,BGCOLOR start="FROMRX" end="TORX"’
Same as above, except that the matching is case insensitive.
‘include "SYNTAXFILE"’
Read in self-contained color syntaxes from "syntaxfile". Note that
"syntaxfile" may contain only the above commands, from ‘syntax’ to
‘icolor’.
‘extendsyntax NAME COMMAND [ARG ...]’
Extend the syntax previously defined as "NAME" with another
COMMAND. This allows you to add a new ‘color’, ‘icolor’, ‘header’,
‘magic’, ‘comment’, ‘linter’, or ‘formatter’ command to an already
defined syntax — useful when you want to slightly improve a syntax
defined in one of the system-installed files (which normally are
not writable).