How to set nano default syntax highlighting for files with no extension?

15,915

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.

Share:
15,915

Related videos on Youtube

AJB
Author by

AJB

Lover of the beach, sharp minds, and distributed systems.

Updated on September 18, 2022

Comments

  • AJB
    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 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?

    • Admin
      Admin about 10 years
      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.
  • Gregory Nisbet
    Gregory Nisbet over 5 years
    I found this answer looking up how to set a "default" syntax highlighting configuration. It works.
  • user3757405
    user3757405 over 5 years
    I suggest adding .nanorc to the extensions list. This file highlights itself quite well.
  • not2qubit
    not2qubit about 5 years
    Unfortunately, using this file make nano extremely slow when navigating through any large conf file.