Kevin's Worklog

Documenting the Daily Stream

Color Coding in Nano

When I’m casually editing a file on the file system I like to use nano. Yes, I admit it. It’s quick and easy. What’s not to like?

Well, it could have color coding. But, wait… it does! I’m a bit surprised I’m just finding this out, but there is a way to enable color coding in nano.

First, you need to find the nanorc.sample file. For me, on Ubuntu, it was gzipped in:

/usr/share/doc/nano/examples/

To use it, I gunzipped it, renamed it, and moved it to my personal directory:

gunzip /usr/share/doc/nano/examples/nanorc.sample.gz -c > ~/.nanorc

Since, at the moment, I’m particularly interested in color coding for JSON files, I added the following to the end of the ~/.nanorc file:

## JSON-type files
include "/usr/share/nano/json.nanorc"

Then I created that file and added the following (found on GitHub) to it:

syntax "json" "\.json$"
header "^\{$"

color blue   "\<[-]?[1-9][0-9]*([Ee][+-]?[0-9]+)?\>"  "\<[-]?[0](\.[0-9]+)?\>"
color cyan  "\<null\>"
color brightcyan "\<(true|false)\>"
color yellow ""(\\.|[^"])*"|'(\\.|[^'])*'"
color brightyellow "\"(\\"|[^"])*\"[[:space:]]*:"  "'(\'|[^'])*'[[:space:]]*:"
color magenta   "\\u[0-9a-fA-F]{4}|\\[bfnrt'"/\\]"
color ,green "[[:space:]]+$"
color ,red "    +"

The next time I open a JSON file, I see pretty color coded syntax. Success!