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?

AJB
  • 187

3 Answers3

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*%?($|&gt;)"

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(=|&gt;)|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 "&quot;(\.|[^"])&quot;" "'(\.|[^'])'"

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.

NickTux
  • 17,865
  • 6
  • 57
  • 66
4

The nanorc file should have

syntax "default"

with no regexp after it.

Jens Erat
  • 5,131
  • 7
  • 33
  • 37
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