How to set nano default syntax highlighting for files with no extension?
Solution 1
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.
Solution 2
The nanorc file should have
syntax "default"
with no regexp after it.
Related videos on Youtube
AJB
Lover of the beach, sharp minds, and distributed systems.
Updated on September 18, 2022Comments
-
AJB over 1 year
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 addedinclude "/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?
-
Admin about 10 yearsNot 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.
-
-
Gregory Nisbet over 5 yearsI found this answer looking up how to set a "default" syntax highlighting configuration. It works.
-
user3757405 over 5 yearsI suggest adding
.nanorc
to the extensions list. This file highlights itself quite well. -
not2qubit about 5 yearsUnfortunately, using this file make nano extremely slow when navigating through any large conf file.