4

I've recently discovered, that when I open a new text file (extension .txt) in nano, there is actually a limited syntax coloring:

enter image description here

That is, if a line starts with number sign/hash, it is colored.

I think this is great, and I really like this (for a txt file, I really don't need any other syntax color, but one marking comment, with the same comment syntax as in bash), so I was wondering how/where it was defined.

According to How to set nano default syntax highlighting for files with no extension?, syntax coloring files for nano are in /usr/share/nano/ directory, and are "imported" via /etc/nanorc file.

So, I tried checking:

$ grep -r txt /usr/share/nano/
/usr/share/nano/cmake.nanorc:syntax cmake "(CMakeLists\.txt|\.cmake)$"

... but the only reference to .txt is from cmake, and I don't think this is what sets the coloring for test.txt.

So, how does nano determine the syntax coloring for a test.txt file?

sdbbs
  • 1,684
  • 2
  • 19
  • 27

1 Answers1

4

Simple grepping for # in the /usr/share/nano/ directory gives the following important result among others:

$ grep -r "#" /usr/share/nano/
...
default.nanorc:color cyan "^[[:space:]]*#.*"
...

The whole file on Ubuntu 20.04 LTS is the following:

$ cat /usr/share/nano/default.nanorc 
## An example of a default syntax.  The default syntax is used for
## files that do not match any other syntax.

syntax default comment "#"

Comments.

color cyan "^[[:space:]]#."

Spaces in front of tabs.

color ,red " + +"

Nano's name, including version.

color brightred "(GNU )?[Nn]ano [1-4].[0-9][-.[:alnum:]]*>"

Email addresses.

color magenta "<[[:alnum:].%_+-]+@[[:alnum:].-]+.[[:alpha:]]{2,}>"

Bracketed captions in certain config files.

color brightgreen "^[[^][]+]$"

So it is some dynamic highlighter in action.
Next things to check trigger - enter GNU nano 4.8 to get it in red and some e-mail <some@email.me> to get it in magenta. Newer versions have URLs in lightblue and so on. See image below:

Nano with plain file

N0rbert
  • 103,263