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:   "\\.?"

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?

  • What happens if you copy the text into ~/.nanorc and not use an include? – mmmmmm Jul 3 '15 at 14:21

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"

From the Nanorc(5) man file:

The configuration file accepts a series of set and unset commands, which can be used to configure nano on startup without using the command line options. Additionally, the syntax, color, and icolor keywords are used to define syntax highlighting rules for different text patterns. nano will read one command per line.

Your nanorc lacks that.

From the git read me from that repo:

Why do I get weird errors when running nano < 2.1.5 on *BSD systems?
In order to reliably highlight keywords, this projects makes heavy use of the GNU regex word boundary extensions (\< and >). BSD implementations also have these extensions but use a different, incompatible syntax ([[:<:]] and [[:>:]]). Since version 2.1.5, nano can automatically translate the GNU syntax to BSD syntax at run-time, but for the benefit of people running a pre-2.1.5 version of nano on OS X or *BSD, the .nanorc file itself can be translated by installing with make BSDREGEX=1.

What is happening is that you did not actually follow the full install commands. If you cloned the git, cd'd into the directory, and ran make install, your syntax files would look different. For one, the default version of nano on OS X (I'm on 10.10.5 Mavericks) is nano 2.0.6. It does not support the header in syntax in this older version. The makefile for the git repo would remove that. And it would add in the color options.

You have 3 options.

  1. Ensure you have the OS X command line tools (or full X Code) installed and run all of the install commands.
  2. use precompiled nanorc syntax files compatible with the preinstalled nano 2.0.6 with BSD regex.
  3. Install a newer nano, and use the nanorc that come with the full install.

As a bonus, I found and edited a single nanorc for sh/bash. Edit as you like:

syntax "sh" "\.sh$" "\.bash$" "bashrc$" "bash_profile$"
icolor brightgreen "^[0-9A-Z_]+\(\)"
color blue "[[:<:]](case|do|done|elif|else|esac|exit|fi|for|function|if|in|local|read|return|select|shift|then|time|until|while)[[:>:]]"
color green "(\{|\}|\(|\)|\;|\]|\[|`|\\|\$|<|>|!|=|&|\|)"
color green "-[Ldefgruwx][[:>:]]"
color green "-(eq|ne|gt|lt|ge|le|s|n|z)[[:>:]]"
color brightblue "[[:<:]](cat|cd|chmod|chown|cp|echo|env|export|grep|install|let|ln|make|mkdir|mv|rm|sed|set|tar|touch|umask|unset)[[:>:]]"
icolor brightred "\$\{?[0-9A-Z_!@#$*?-]+\}?"
color cyan "(^|[[:space:]])#.*$"
color brightmagenta ""(\\.|[^"])*"" "'(\\.|[^'])*'"
color ,green "[[:space:]]+$"

You must log in to answer this question.