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:

# 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.

Posted at 17:05:28 GMT-0700

Category : CodeFreeBSDHowTo

Tags :

Leave a Reply

6 Views