Score:3

How does nano determine syntax coloring for a .txt file?

id flag

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?

Score:2
zw flag

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 <[email protected]> to get it in magenta. Newer versions have URLs in lightblue and so on. See image below:

Nano with plain file

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.