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