Does nano select the wrong syntax highlighter? But how to know which?

Tuesday, December 16, 2025 

Ever have a problem where you open a file, say httpd-vhosts.conf, and it is highlighted with the wrong syntax highlighter? Unlike vim, nano won’t tell you which highlighter it is using. You could, of course, disable each one-by-one until you get the correct highlighting or maybe this little bash script will come in handy (freebsd location version):

for file in /usr/local/share/nano/*.nanorc; do
    syntax_line=$(grep '^syntax ' "$file" | head -1)
    if [ -n "$syntax_line" ]; then
        echo "$syntax_line" | grep -oE '"[^"]+"' | while read -r quoted_pattern; do
            pattern=$(echo "$quoted_pattern" | tr -d '"')
            if echo "httpd-vhosts.conf" | grep -qE "$pattern" 2>/dev/null; then
                echo "MATCH: $file"
                echo "  Pattern: $pattern"
                echo "  Full syntax line: $syntax_line"
                echo
            fi
        done
    fi
done

Modify the if echo "httpd-vhosts.conf" line as appropriate to your use case.  It should return something like:

MATCH: /usr/local/share/nano/apacheconf.nanorc
  Pattern: httpd\.conf|httpd-vhosts\.conf|mime\.types|vhosts\.d\\*|\.htaccess|httpd-.*\.conf$
  Full syntax line: syntax "Apacheconf" "httpd\.conf|httpd-vhosts\.conf|mime\.types|vhosts\.d\\*|\.htaccess|httpd-.*\.conf$"

MATCH: /usr/local/share/nano/etc-hosts.nanorc
  Pattern: hosts
  Full syntax line: syntax "/etc/hosts" "hosts"

Yep, that’ll do it.  Fixed.

What the script does is parse every syntax highlighter file (*.nanorc) for the detection regular expression then test that extracted regular expression against the filename of the file you’re trying to syntax highlight, in this case httpd-vhosts.conf and shows you which blah.nanorc files think they’ll match. There must be only one, or you may get unexpected syntax highlighting.

Note that the location might be different on your system and not /usr/local/share/nano/*.nanorc but rather (linux) /usr/share/nano/*.nanorc or in your personal directory, possibly ~/.nanorc/*.nanorc

Posted at 17:05:28 GMT-0700

Category: CodeFreeBSDHowTo