Does nano select the wrong syntax highlighter? But how to know which?
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
-
Recent Posts
- Generative Spongiform Encephalopathy (GSE): The AI Ouroboros 2026 January 11
- Free your Datas from The Tyranny of the Organizer 2026 January 09
- Does nano select the wrong syntax highlighter? But how to know which? 2025 December 16
- Serial dependencies are a catastrophe already starting to happen 2025 November 19
- Cleaning up poor quality laser scans with Meshlab 2025 September 12
- Disappearing checks in WordPress checkboxes 2025 July 29
- Apropos of nothing in particular…. 2025 March 26
- Treegraph.sh a tool for generating pretty file structure graphs 2025 February 28
- FINAL System shutdown after 16 years 2025 February 12
- Adding a feature to MediaWiki WikiEditor formatting 2025 January 18
- Categories
- Links
- Search
- Archives
- Post History
December 2025 M T W T F S S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31