How to use syntax highlighting with the GNU nano text editor

The GNU nano text editor is a very small but useful text editor.

Files to edit
You can edit the file /etc/nanorc or the file ~/.nanorc to add some syntax for the files that you want.

Explaining how syntax highlight work
You first need to specify a line with the name of the syntax and the files that you want to be highlighted by your syntax. Adding a line like : syntax "c-file" "\.(c|h)$" means that you want all the .c and .h files to be highlighted with the content that you specify after this line, with some regular expressions. For example, add the following line after the previous line to highlight all the integer numbers (positive or negative) in red : color red "\<[-+]?([1-9][0-9]*|0)?\>"

Pre made nanorc file
This file text should be placed into a file named "/etc/nanorc". If you are not root, then into "~/.nanorc".

It will enable colors for various type files.


 * 1) Sample initialization file for GNU nano.
 * 2) Please note that you must have configured nano with --enable-nanorc
 * 3) for this file to be read!  Also note that this file should not be in
 * 4) DOS or Mac format, and that characters specially interpreted by the
 * 5) shell should not be escaped here.
 * 6) To make sure a value is disabled, use "unset ".
 * 7) For the options that take parameters, the default value is given.
 * 8) Other options are unset by default.
 * 9) Quotes inside string parameters don't have to be escaped with
 * 10) backslashes.  The last double quote in the string will be treated as
 * 11) its end.  For example, for the "brackets" option, ""')>]}" will match
 * ", ', ), >, ], and }.
 * 1) Quotes inside string parameters don't have to be escaped with
 * 2) backslashes.  The last double quote in the string will be treated as
 * 3) its end.  For example, for the "brackets" option, ""')>]}" will match
 * ", ', ), >, ], and }.


 * 1) Use auto-indentation.
 * 2) set autoindent


 * 1) Backup files to filename~.
 * 2) set backup


 * 1) The directory to put unique backup files in.
 * 2) set backupdir ""


 * 1) Do backwards searches by default.
 * 2) set backwards

set boldtext
 * 1) Use bold text instead of reverse video text.

set brackets ""')>]}"
 * 1) The characters treated as closing brackets when justifying
 * 2) paragraphs.  They cannot contain blank characters.  Only closing
 * 3) punctuation, optionally followed by closing brackets, can end
 * 4) sentences.


 * 1) Do case sensitive searches by default.
 * 2) set casesensitive


 * 1) Constantly display the cursor position in the statusbar.  Note that
 * 2) this overrides "quickblank".
 * 3) set const


 * 1) Use cut to end of line by default.
 * 2) set cut


 * 1) Set the line length for wrapping text and justifying paragraphs.
 * 2) If fill is 0 or less, the line length will be the screen width less
 * 3) this number.
 * 4) set fill -8
 * 1) set fill -8

set historylog
 * 1) Enable ~/.nano_history for saving and reading search/replace strings.

set matchbrackets "(<[{)>]}"
 * 1) The opening and closing brackets that can be found by bracket
 * 2) searches.  They cannot contain blank characters.  The former set must
 * 3) come before the latter set, and both must be in the same order.


 * 1) Use the blank line below the titlebar as extra editing space.
 * 2) set morespace

set mouse
 * 1) Enable mouse support, if available for your system.  When enabled,
 * 2) mouse clicks can be used to place the cursor, set the mark (with a
 * 3) double click), and execute shortcuts.  The mouse will work in the X
 * 4) Window System, and on the console when gpm is running.


 * 1) Allow multiple file buffers (inserting a file will put it into a
 * 2) separate buffer).  You must have configured with --enable-multibuffer
 * 3) for this to work.
 * 4) set multibuffer
 * 1) set multibuffer


 * 1) Don't convert files from DOS/Mac format.
 * 2) set noconvert


 * 1) Don't follow symlinks when writing files.
 * 2) set nofollow


 * 1) Don't display the helpful shortcut lists at the bottom of the screen.
 * 2) set nohelp


 * 1) Don't add newlines to the ends of files.
 * 2) set nonewlines


 * 1) Don't wrap text at all.
 * 2) set nowrap


 * 1) Set operating directory.  nano will not read or write files outside
 * 2) this directory and its subdirectories.  Also, the current directory
 * 3) is changed to here, so any files are inserted from this dir.  A blank
 * 4) string means the operating directory feature is turned off.
 * 5) set operatingdir ""
 * 1) set operatingdir ""


 * 1) Preserve the XON and XOFF keys (^Q and ^S).
 * 2) set preserve


 * 1) The characters treated as closing punctuation when justifying
 * 2) paragraphs.  They cannot contain blank characters.  Only closing
 * 3) punctuation, optionally followed by closing brackets, can end
 * 4) sentences.
 * 5) set punct "!.?"
 * 1) set punct "!.?"


 * 1) Do quick statusbar blanking.  Statusbar messages will disappear after
 * 2) 1 keystroke instead of 26.  Note that "const" overrides this.
 * 3) set quickblank
 * 1) set quickblank


 * 1) The email-quote string, used to justify email-quoted paragraphs.
 * 2) This is an extended regular expression if your system supports them,
 * 3) otherwise a literal string.  Default:
 * 4) set quotestr "^([ 	]*[#:>\|}])+"
 * 5) if you have extended regular expression support, otherwise:
 * 6) set quotestr "> "


 * 1) Fix Backspace/Delete confusion problem.
 * 2) set rebinddelete


 * 1) Fix numeric keypad key confusion problem.
 * 2) set rebindkeypad

set regexp
 * 1) Do extended regular expression searches by default.


 * 1) Make the Home key smarter.  When Home is pressed anywhere but at the
 * 2) very beginning of non-whitespace characters on a line, the cursor
 * 3) will jump to that beginning (either forwards or backwards).  If the
 * 4) cursor is already at that position, it will jump to the true
 * 5) beginning of the line.
 * 6) set smarthome


 * 1) Use smooth scrolling as the default.
 * 2) set smooth

set speller "aspell -x -c"
 * 1) Use this spelling checker instead of the internal one.  This option
 * 2) does not properly have a default value.

set suspend
 * 1) Allow nano to be suspended.

set tabsize 4
 * 1) Use this tab size instead of the default; it must be greater than 0.


 * 1) Convert typed tabs to spaces.
 * 2) set tabstospaces


 * 1) Save automatically on exit, don't prompt.
 * 2) set tempfile


 * 1) Disallow file modification; why would you want this in an rcfile? ;)
 * 2) set view

set whitespace ". "
 * 1) The two single-column characters used to display the first characters
 * 2) of tabs and spaces.  187 in ISO 8859-1 (0000BB in Unicode) and 183 in
 * 3) ISO-8859-1 (0000B7 in Unicode) seem to be good values for these.


 * 1) Detect word boundaries more accurately by treating punctuation
 * 2) characters as part of a word.
 * 3) set wordbounds


 * 1) Color setup
 * 2) Format:
 * 3) syntax "short description" ["filename regex" ...]
 * 4) The "none" syntax is reserved; specifying it on the command line is
 * 5) the same as not having a syntax at all.  The "default" syntax is
 * 6) special: it takes no filename regexes, and applies to files that
 * 7) don't match any other syntax's filename regexes.
 * 8) color foreground,background "regex" ["regex"...]
 * 9) or
 * 10) icolor foreground,background "regex" ["regex"...]
 * 11) "color" will do case sensitive matches, while "icolor" will do case
 * 12) insensitive matches.
 * 13) Valid colors: white, black, red, blue, green, yellow, magenta, cyan.
 * 14) For foreground colors, you may use the prefix "bright" to get a
 * 15) stronger highlight.
 * 16) To use multi-line regexes, use the start="regex" end="regex"
 * 17) [start="regex" end="regex"...] format.
 * 18) If your system supports transparency, not specifying a background
 * 19) color will use a transparent color.  If you don't want this, be sure
 * 20) to set the background color to black or white.
 * 21) If you wish, you may put your syntaxes in separate files.  You can
 * 22) make use of such files (which can only include "syntax", "color", and
 * 23) "icolor" commands) as follows:
 * 24) include "/path/to/syntax_file.nanorc"
 * 25) Unless otherwise noted, the name of the syntax file (without the
 * 26) ".nanorc" extension) should be the same as the "short description"
 * 27) name inside that file.  These names are kept fairly short to make
 * 28) them easier to remember and faster to type using nano's -Y option.
 * 29) All regexes should be extended regular expressions.
 * 1) If you wish, you may put your syntaxes in separate files.  You can
 * 2) make use of such files (which can only include "syntax", "color", and
 * 3) "icolor" commands) as follows:
 * 4) include "/path/to/syntax_file.nanorc"
 * 5) Unless otherwise noted, the name of the syntax file (without the
 * 6) ".nanorc" extension) should be the same as the "short description"
 * 7) name inside that file.  These names are kept fairly short to make
 * 8) them easier to remember and faster to type using nano's -Y option.
 * 9) All regexes should be extended regular expressions.
 * 1) them easier to remember and faster to type using nano's -Y option.
 * 2) All regexes should be extended regular expressions.
 * 1) All regexes should be extended regular expressions.

include "/usr/share/nano/nanorc.nanorc"
 * 1) Nanorc files

include "/usr/share/nano/c.nanorc"
 * 1) C/C++

include "/usr/share/nano/html.nanorc"
 * 1) HTML

include "/usr/share/nano/tex.nanorc"
 * 1) TeX

include "/usr/share/nano/mutt.nanorc"
 * 1) Quoted emails (under e.g. mutt)

include "/usr/share/nano/patch.nanorc"
 * 1) Patch files

include "/usr/share/nano/man.nanorc"
 * 1) Manpages

include "/usr/share/nano/groff.nanorc"
 * 1) Groff

include "/usr/share/nano/perl.nanorc"
 * 1) Perl

include "/usr/share/nano/python.nanorc"
 * 1) Python

include "/usr/share/nano/ruby.nanorc"
 * 1) Ruby

include "/usr/share/nano/java.nanorc"
 * 1) Java

include "/usr/share/nano/asm.nanorc"
 * 1) Assembler

include "/usr/share/nano/sh.nanorc"
 * 1) Bourne shell scripts

syntax "xorg" "xorg\.conf$" color brightwhite "(Section|EndSection|Sub[sS]ection|EndSub[sS]ection)" color yellow "[^A-Za-z0-9](Identifier|Screen|InputDevice|Option|RightOf|LeftOf|Driver|RgbPath|FontPath|ModulePath|Load|VendorName|ModelName|BoardName|BusID|Device|Monitor|DefaultDepth|View[pP]ort|Depth|Virtual|Modes|Mode|DefaultColorDepth|Modeline|\+vsync|\+hsync|HorizSync|VertRefresh)[^A-Za-z0-9]" color magenta "[0-9]" color green ""(\\.|[^\"])*"" color blue "#.*"
 * 1) syntax highlighting in xorg.conf
 * 1) keywords
 * 1) numbers
 * 1) strings
 * 1) comments


 * 1) Syntax highlighting for XML files
 * 2) Author:  Josef 'Jupp' Schugt, jupp(a)rubyforge.org
 * 3) License: GPL 2  or later
 * 4) Version: 2004-02-25
 * 1) Author:  Josef 'Jupp' Schugt, jupp(a)rubyforge.org
 * 2) License: GPL 2  or later
 * 3) Version: 2004-02-25
 * 1) Version: 2004-02-25

syntax "ml" ".*\.([jrs]?html?|xml|sgml?)$" color white "^.+$" color green start="<" end=">" color cyan  "<[^> ]+" color cyan  ">" color yellow start="<!DOCTYPE" end="[/]?>" color yellow start="" color red   "&[^;]*;"


 * 1) Syntax highlighting for HTTP codes
 * 2) Author:  Josef 'Jupp' Schugt, jupp(a)rubyforge.org
 * 3) License: GPL 2  or later
 * 4) Version: 2004-02-25
 * 1) Author:  Josef 'Jupp' Schugt, jupp(a)rubyforge.org
 * 2) License: GPL 2  or later
 * 3) Version: 2004-02-25
 * 1) Version: 2004-02-25

syntax "urls" color brightmagenta  "^.*$" color cyan     "^(1[0-9][0-9]|20[256]|30[45]).*$" color green     "^20[03].*$" color brightyellow  "^(201|30[0-37]).*$" color brightred     "^(204|[45][0-9][0-9]|666).*$"


 * 1) Syntax highlighting for CSS files
 * 2) Author:  Simon Rupf, simon.rupf(a)int-ag.ch
 * 3) License: GPL 2  or later
 * 4) Version: 2005-02-14
 * 1) Author:  Simon Rupf, simon.rupf(a)int-ag.ch
 * 2) License: GPL 2  or later
 * 3) Version: 2005-02-14
 * 1) Version: 2005-02-14

syntax "css" "\.css$" color brightred    "." color brightyellow start="\{" end="\}" color brightwhite          start=":" end="[;^\{]" color brightblue   ":active|:focus|:hover|:link|:visited|:link|:after|:before|$" color brightblue   start="\/\*" end="\\*/" color green        ";|:|\{|\}"