Nano: how to obtain syntax highlighting

bashterminal

I have a default installation of nano under /usr/bin/, and I am trying to make nano highlight the bash scripting language code.

I have been searching around, but I had no success: I got always some errors, or simply doesn't work. For example, I have tried to download the following repository from Github github.com/nanorc/nanorc.git with the following command:

git clone https://github.com/nanorc/nanorc.git

and then follow the steps in this post, but I have had errors because some command inside the files are not recognised. In particular, if I add this line include ~/nanorc/shell.nanorc to my file ~/.nanorc, where the file shell.nanorccontains:

syntax "sh" "\.sh$" "\.bash$" "bashrc$" "bash_profile$"
header "^#!.*/(env +)?(ba|k|pdk)?sh[-0-9_]*( |$)"

FUNCTION: "^[A-Za-z0-9_+-]+[[:space:]]*\(\)"
KEYWORD:  "\<(case|do|done|elif|else|esac|fi|for|function|if)\>"
KEYWORD:  "\<(in|select|then|time|until|while)\>"
FUNCTION: "\<(echo|exit|export|let|local|read|return|set|shift|umask|unset)\>"
FUNCTION: "\<(source)\>"
OPERATOR: "[{}()<>!=&|;`$]|\\|\[|\]|-(eq|ne|gt|lt|ge|le|s|n|z)\>"
STRING:   ""(\\.|[^"])*""
VARIABLE: "\$\{?[A-Za-z0-9_!@#$*?-]+\}?|`[^`]*`"
STRING:   "'(\\.|[^'])*'"
ESCAPE:   "\\.?"
+HASHCOMMENT
+TODO
+LINT

It seems that these previous commands, such STRING, ESCAPE, etc, are not recognised.

How would you set syntax highlighting for nano, at least for bash?

Best Answer

Install the latest version of nano with homebrew:

  1. Install homebrew: https://brew.sh/
  2. Install nano: brew install nano
  3. Add this line to ~/.nanorc. That directory has all the syntax highlighting definition files.

    include "/usr/local/share/nano/*.nanorc"