6

I'm trying to set a 'default' syntax highlighting scheme for nano when I'm editing files with no extension such as /etc/nginx/sites-available/default or /etc/ssh/sshd_config but I can't seem to make it work.

I've copied the c syntax highlighting file into /usr/share/nano/default.nanorc and then added include "/usr/share/nano/deafult.nanorc" to my ~/.nanorc file but I'm still not getting highlighting for files with no extension.

Anyone know how to enable this?

1
  • 2
    Not a dupe. I followed the instructions in the linked question to get syntax highlighting working for several popular extensions. But I'm trying to get highlighting working for file with no extension.
    – AJB
    Feb 4, 2014 at 18:51

3 Answers 3

9

You can define this by yourself. Here is a good example from Arch Linux Forums.

Copy the following code and save it in /usr/share/nano/ as conf.nanorc

# config file highlighting

syntax "conf" "(\.(conf|config|cfg|cnf|rc|lst|list|defs|ini|desktop|mime|types|preset|cache|seat|service|htaccess)$|(^|/)(\w*crontab|mirrorlist|group|hosts|passwd|rpc|netconfig|shadow|fstab|inittab|inputrc|protocols|sudoers)$|conf.d/|.config/)"

# default text
color magenta "^.*$"
# special values
icolor brightblue "(^|\s|=)(default|true|false|on|off|yes|no)(\s|$)"
# keys
icolor cyan "^\s*(set\s+)?[A-Z0-9_\/\.\%\@+-]+\s*([:]|\>)"
# commands
color blue "^\s*set\s+\<"
# punctuation
color blue "[.]"
# numbers
color red "(^|\s|[[/:|<>(){}=,]|\])[-+]?[0-9](\.?[0-9])*%?($|\>)"
# keys
icolor cyan "^\s*(\$if )?([A-Z0-9_\/\.\%\@+-]|\s)+="
# punctuation
color blue "/"
color brightwhite "(\]|[()<>[{},;:=])"
color brightwhite "(^|\[|\{|\:)\s*-(\s|$)"
# section headings
icolor brightyellow "^\s*(\[([A-Z0-9_\.-]|\s)+\])+\s*$"
color brightcyan "^\s*((Sub)?Section\s*(=|\>)|End(Sub)?Section\s*$)"
color brightcyan "^\s*\$(end)?if(\s|$)"
# URLs
icolor green "\b(([A-Z]+://|www[.])[A-Z0-9/:#?&$=_\.\-]+)(\b|$| )"
# XML-like tags
icolor brightcyan "</?\w+((\s*\w+\s*=)?\s*("[^"]*"|'[^']*'|!?[A-Z0-9_:/]))*(\s*/)?>"
# strings
color yellow "\"(\\.|[^"])*\"" "'(\\.|[^'])*'"
# comments
color white "#.*$"
color blue "^\s*##.*$"
color white "^;.*$"
color white start="<!--" end="-->"

Then include this configuration in /etc/nanorc file as

## Configuration files (catch-all syntax)
include "/usr/share/nano/conf.nanorc"

The first code line in the snippet includes a regular expression that defines for which file names this syntax highlighting should be used. Whenever you encounter a config file that is not matched by this, but you would still like to open it with syntax highlighting, you can manually select this syntax with nano's -Y switch, like so:

nano -Y conf myConfigFile

Source: nano syntax highlighting: catch-all syntax for configuration files.

2
  • 1
    I suggest adding .nanorc to the extensions list. This file highlights itself quite well.
    – mwfearnley
    Dec 18, 2018 at 15:57
  • Unfortunately, using this file make nano extremely slow when navigating through any large conf file.
    – not2qubit
    Feb 11, 2019 at 14:52
4

The nanorc file should have

syntax "default"

with no regexp after it.

1
  • 1
    I found this answer looking up how to set a "default" syntax highlighting configuration. It works. Aug 11, 2018 at 22:38
0

Nano syntax highlight all files with and without extension:

Create backup from default file

yes | cp /usr/share/nano/default.nanorc /usr/share/nano/default.nanorc.backup

Create a copy from a config file

yes | cp /usr/share/nano/sh.nanorc /usr/share/nano/default.nanorc

Change syntax extension name to default

sed -i -e 's/syntax "sh" "\\.sh$"/syntax "default"/g' /usr/share/nano/default.nanorc

Store all .nanrc file in the include file

ls -1 /usr/share/nano/*.nanorc | sed 's/^\//include \//' >> ~/.nanorc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.