Nano Syntax Highlighting
From Linuxhelp Wiki
| Author: | Corey |
| Created On: | February 2nd, 2005 |
| Updated On: | Not updated |
Contents |
Introduction
Nano is one of the most popular console-based text editors available for Linux. It's easy to use for new users, however provides good functionality for experienced users and programmers. It is a little-known fact that you can actually configure syntax highlighting with nano which is useful for any programmer, developer, or even a common user who is editing configuration files.
nanorc
All configuration is done through the nanorc file. Depending on your distribution, you should find an example file in /usr/doc/nano-x.x.x/nanorc.sample.gz , or if you compiled nano from source, you may find it in /usr/local/doc/nano-x.x.x/nanorc.sample.gz.
The first thing we need to do is to copy this sample file to our home directory which can be done with:
zcat /usr/doc/nano-x.x.x/nanorc.sample.gz >~/.nanorc
This will copy the sample file to our home directory.
There are many things in the nanorc file that not only pertain to syntax highlighting, but also to other features like autoindent, backups, enabling mouse support, turning off word-wrap, etc.
Towards the bottom of your nanorc file, you will notice that are are some samples of syntax highlighting. If you would like to use these, then just un-comment out the line (delete the '#' at the begining of the line), save the file, and run nano on a file with syntax.
Syntax Highlighting Code
The following are blocks of code that you can add to your ~/.nanorc file to enable syntax highlighting. If you plan on working with any of these types of files, copy the blocks of code to the bottom of your configuration file.
One important thing to keep in mind when using or adapting any of the syntax highlighting code is that the process happens in the order the code is listed. E.g.
color white "('[^']*')|(\"[^"]*\")"
color blue start="<[^\?]" end="[^\?]>"
and
color blue start="<[^\?]" end="[^\?]>"
color white "('[^']*')|(\"[^"]*\")"
give very different results (the former makes everything in angle brackets blue, and the latter makes everything in angle brackets blue except for things in quotes, which are white).
PHP
syntax "php" "\.php[2345s~]?$"
color brightblue "(.*)\("
color blue "\$[a-zA-Z_0-9$]*|[=!<>]"
color green "(var|class|function|echo|case|break|default|exit|switch|if|else|elseif|@|while)\s"
color green "[.,{}();]"
color red "('[^']*')|(\"[^"]*\")"
color brightyellow "(#.*|//.*)$"
color brightyellow start="/\*" end="\*/"
color brightblue "(<\?(php)?|\?>)"
color white start="\?>" end="<\?(php)?"
Alternate PHP
syntax "php" "\.php[2345s~]?$"
color brightblue start="<\?(php)?" end="\?>"
color blue start="<[^\?]" end="[^\?]>"
color cyan "\$[a-zA-Z_0-9]*"
color cyan "(\[)|(\])"
color red "(var|class|function|echo|case|break|default|exit|switch|if|else|elseif|@|while)\s"
color red "="
color green "[,{}()]"
color green "=="
color white "('[^']*')|(\"[^"]*\")"
color yellow start="<!--" end="-->"
color yellow start="/\*" end="\*/"
color yellow start="#" end="$"
Alternate PHP
syntax "php" "\.php[2345s~]?$"
color blue "\<(var|class|function|echo|case|break|default|exit|switch|if|else|elseif|@|while|foreach|return|public|private|protected|require|require_once|new|true|false|as)\>"
color red "<\?(php)|\?>"
color red "\$[a-zA-Z_0-9]*"
color red "[0-9]*"
color cyan "('[^']*')|(\"[^"]*\")"
color yellow start=""
color yellow start="/\*" end="\*/"
color yellow start="#" end="$"
Python
syntax "python" "\.py$" color brightblue "def [a-zA-Z_0-9]+" color brightcyan "\<(and|assert|break|class|continue|def|del|elif|else|except|exec|finally|for|from|global|if|import|in|is|lambda|map|not|or|pass|print|raise|return|try|while)\>" color brightgreen "([\"']).*?[^\\]\1" color brightgreen start="\"\"\"" end="\"\"\"" color brightred "#.*$"
C file
syntax "c-file" "\.(c|h)$"
color red "\<[A-Z_]{2,}\>"
color green "\<(float|double|char|int|short|long|enum|void|static|const|struct|union|typedef|extern|signed|unsigned|inline)\>"
color brightyellow "\<(for|if|while|do|else|case|switch|goto|continue|break|return)\>"
color brightcyan "^ *# *(define|undef|include|ifn?def|endif|elif|else|if)"
color brightmagenta "'([^\]|(\\['abfnrtv\\]))'" "'\\(([0-7][0-7]?)|([0-3][0-7][0-7]))'" "'\\x[0-9A-Fa-f][0-9A-Fa-f]?'"
color brightyellow "<[^= ]*>" ""(\\.|[^\"])*""
color brightyellow start=""(\\.|[^\"])*\\ *$" end="^(\\.|[^\"])*""
color brightblue "//.*"
color brightblue start="/\*" end="\*/"
Alt C/C++/h
syntax "c" "\.(c|C|cc|cpp|cxx|h|H|hh|hpp|hxx)$" ## Preprocessor directives (Anything that begins with #) color brightblack "#+(.*)" ## ## General syntax color brightred "\<[A-Z_][0-9A-Z_]+\>" color green "\<(float|double|bool|char|int|short|long|sizeof|enum|void|static|const|struct|union|typedef|extern|signed|unsigned|inline)\>" color green "\<(u_?)?int(8|16|32|64|ptr)_t\>" color green "\<(class|namespace|template|public|protected|private|typename|this|friend|virtual|using|mutable|volatile|register|explicit)\>" color brightyellow "\<(for|if|while|do|else|case|default|switch)\>" color brightyellow "\<(try|throw|catch|operator|new|delete)\>" color magenta "\<(goto|continue|break|return|throw)\>" color brightcyan "^space:*#space:*(define|undef|include|ifn?def|endif|elif|else|if|warning|error)" color brightmagenta "'([^'\]|(\\["'abfnrtv\\]))'" "'\\(([0-3]?[0-7]{1,2}))'" "'\\x[0-9A-Fa-f]{1,2}'" ## ## GCC builtins color cyan "__attribute__space:*\(\([^)]*\)\)" "__(aligned|asm|builtin|hidden|inline|packed|restrict|section|typeof|weak)__" ## ## String highlighting. You will in general want your comments and ## strings to come last, because syntax highlighting rules will be ## applied in the order they are read in. color brightyellow "<[^= ]*>" ""(\\.|[^"])*"" ## ## This string is VERY resource intensive! color brightyellow start=""(\\.|[^"])*\\space:*$" end="^(\\.|[^"])*"" ## ## Comment highlighting color brightblue "//.*" color brightblue start="/\*" end="\*/"
HTML
syntax "HTML" "\.html$" color blue start="<" end=">" color red "&[^; ]*;"
TeX
syntax "TeX" "\.tex$"
color green "\\.|\\[A-Za-z]*"
color magenta "[{}]"
color blue "%.*"
Groff
syntax "groff" "\.ms$" "\.mm$" "\.me$" "\.tmac$" "^tmac." ".rof" color cyan "^\.ds [^ ]*" color cyan "^\.nr [^ ]*" color brightmagenta "\\." color brightmagenta "\\f." color brightmagenta "\\f\(.." color brightmagenta "\\s(\+|\-)?[0-9]" color cyan "(\\|\\\\)n." color cyan "(\\|\\\\)n\(.." color cyan start="(\\|\\\\)n\[" end="]" color brightgreen "^\. *[^ ]*" color yellow "^\.\\\".*$" color green "(\\|\\\\)\*." color green "(\\|\\\\)\*\(.." color green start="(\\|\\\\)\*\[" end="]" color brightred "\\\(.." color brightred start="\\\[" end="]" color brightcyan "\\\\\$[1-9]"
Perl
syntax "perl" "\.p[lm]$" color red "\<(accept|alarm|atan2|bin(d|mode)|c(aller|h(dir|mod|op|own|root)|lose(dir)?|onnect|os|rypt)|d(bm(close|open)|efined|elete|ie|o|ump)|e(ach|of|val|x(ec|ists|it|p))|f(cntl|ileno|lock|ork)|get(c|login|peername|pgrp|ppid|priority|pwnam|(host|net|proto|serv)byname|pwuid|grgid|(host|net)byaddr|protobynumber|servbyport)|([gs]et|end)(pw|gr|host|net|proto|serv)ent|getsock(name|opt)|gmtime|goto|grep|hex|index|int|ioctl|join|keys|kill|last|length|link|listen|local(time)?|log|lstat|m|mkdir|msg(ctl|get|snd|rcv)|next|oct|open(dir)?|ord|pack|pipe|pop|printf?|push|q|qq|qx|rand|re(ad(dir|link)?|cv|do|name|quire|set|turn|verse|winddir)|rindex|rmdir|s|scalar|seek|seekdir|se(lect|mctl|mget|mop|nd|tpgrp|tpriority|tsockopt)|shift|shm(ctl|get|read|write)|shutdown|sin|sleep|socket(pair)?|sort|spli(ce|t)|sprintf|sqrt|srand|stat|study|substr|symlink|sys(call|read|tem|write)|tell(dir)?|time|tr|y|truncate|umask|un(def|link|pack|shift)|utime|values|vec|wait(pid)?|wantarray|warn|write)\>" color magenta "\<(continue|else|elsif|do|for|foreach|if|unless|until|while|eq|ne|lt|gt|le|ge|cmp|x|my|our|sub|use|package|can|isa)\>" color cyan start="[$@%]" end="( |\\W|-)" color yellow ""[^"]*"|qq\|.*\|" color yellow "'[^']*'" color white "[sm]/.*/" color white start="(^use| = new)" end=";" color green "#.*" color yellow start="<< 'STOP'" end="STOP"
Java
syntax "Java source" "\.java$" color green "\<(boolean|byte|char|double|float|int|long|new|short|this|transient|void)\>" color red "\<(break|case|catch|continue|default|do|else|finally|for|if|return|switch|throw|try|while)\>" color cyan "\<(abstract|class|extends|final|implements|import|instanceof|interface|native|package|private|protected|public|static|strictfp|super|synchronized|throws|volatile)\>" color red ""[^\"]*"" color yellow "\<(true|false|null)\>" color blue "//.*" color blue start="/\*" end="\*/" color brightblue start="/\*\*" end="\*/" color brightgreen,green " +$"
Patch Files
syntax "patch" "\.(patch|diff)$" color brightgreen "^\+.*" color green "^\+\+\+.*" color brightblue "^ .*" color brightred "^-.*" color red "^---.*" color brightyellow "^@@.*" color magenta "^diff.*"
Gentoo Ebuilds
syntax "ebuild" "\.ebuild$" color brightcyan "^ *eclass\>" "^ *inherit\>" color brightwhite "^ *src [^ ]*" color brightblue "src_compile\>" "src_unpack\>" "src_install\>" "^ *einfo\>" "$ color yellow "^ *export\>" color magenta "\<(if|then|fi)\>" color green "#.*$"
Makefile
syntax "makefile" "^Makefile\.?"
color red "[:=]"
color magenta "\<(if|ifeq|else|endif)\>"
color blue "\$\{.*}"
color brightblue "\w+:"
color green "#.*$"
JavaScript
syntax "JavaScript" "\.(js)$" # Default color white "^.+$" # Decimal, octal and hexadecimal numbers color yellow "\<[-+]?([1-9][0-9]*|0[0-7]*|0x[0-9a-fA-F]+)([uU][lL]?|[lL][uU]?)?\>" # Floating point number with at least one digit before decimal point color yellow "\<[-+]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)([EePp][+-]?[0-9]+)?[fFlL]?" color yellow "\<[-+]?([0-9]+[EePp][+-]?[0-9]+)[fFlL]?" # Keywords color green "\<(break|case|catch|continue|default|delete|do|else|finally)\>" color green "\<(for|function|if|in|instanceof|new|null|return|switch)\>" color green "\<(switch|this|throw|try|typeof|undefined|var|void|while|with)\>" # Type specifiers color red "\<(Array|Boolean|Date|Enumerator|Error|Function|Math)\>" color red "\<(Number|Object|RegExp|String)\>" color red "\<(true|false)\>" # String color brightyellow "L?\"(\\"|[^"])*\"" color brightyellow "L?'(\'|[^'])*'" # Escapes color red "\\[0-7][0-7]?[0-7]?|\\x[0-9a-fA-F]+|\\[bfnrt'"\?\\]" # Comments color magenta start="/\*" end="\*/" color magenta "//.*$"
postgreSQL
syntax "postgreSQL" "\.sql$"
icolor red "\<(A(LL|NALY(S|Z)E|ND|NY|RRAY|S|SC|SYMMETRIC|UTHORIZATION)|B(ETWEEN|INARY|OTH|Y)|C(ASE|AST|HECK|OLLATE|OLUMN|ONSTRAINT|REATE|ROSS|URRENT_(DATE|ROLE|TIME|TIMESTAMP|USER))|D(EFAULT|EFERRABLE|ESC|ISTINCT|O|ROP)|E(LSE|ND|XCEPT)|F(ALSE|OR(EIGN)?|REEZE|ROM|ULL)|GRANT|GROUP|HAVING|I(LIKE|N(ITIALLY|NER|TERSECT|TO)?|S|SNULL)|JOIN|L(EADING|EFT|IKE|IMIT|OCALTIME(STAMP)?)|N(ATURAL|EW|OT(NULL)?|ULL)|O(FF(SET)?|LD|N|NLY|R|RDER|UTER|VERLAPS)|PLACING|PRIMARY|REFERENCES|RIGHT|S(ELECT|ESSION_USER|IMILAR|OME|YMMETRIC)|T(ABLE|HEN|O|RAILING|RUE)|UNION|UNIQUE|USER|USING|VE(IW|RBOSE)|WHEN|WHERE|)\>"
color magenta "(\{|\}|\(|\)|\;|\]|\[|`|\\|\$|<|>|!|=|&|\|)"
color yellow "'[^']*'"
color green "--.*$"

