I'm trying to customize nano to my needs, I copied nanorc from the /etc directory in my home directory and haven't edited anything so far.

My goal is to enable syntax highlighting for config files such as .bashrc. Syntax highlighting already works when I assign an extension to files such as file.py but it doesn't work for config files which are usually full of comments and options unless I explicitly type nano configfile --syntax=sh. I would like this flag to be enabled by default.

When looking into the nanorc file I noticed the statement include /usr/share/nano/*.nanorc. When sourcing the file with source .nanorc I get the following error:

.nanorc:236: command not found: include

Why is this happening and how can I fix it? Thanks!

  • 3
    Because .nanorc files aren't shell scripts? they are meant to be read and parsed internally by nano itself - not by your shell – steeldriver Feb 16 '18 at 18:35
  • I did not know that.. but then how can I solve the problem I have? – mickkk Feb 16 '18 at 18:37
  • Can you be more specific than "config files"? what kind of files are these? do they really have a Bourne shell-like syntax? – steeldriver Feb 16 '18 at 18:44
  • 1
    @steeldriver they're simply text files. Anyway I fixed it! Your comment helped me understanding that since they're not shell scripts, nano uses the default syntax settings! So I edited /usr/share/nano/default.nanorc including the desired default syntax! Thanks! I don't know why, but I was assuming they were all shell scripts by default.. – mickkk Feb 16 '18 at 19:03
  • 3
    OK sounds like you should write an answer based on your findings! – steeldriver Feb 16 '18 at 19:06

I found a solution to the problem!

In order to perform syntax highlighting, nano uses a set of configuration files which located (in Ubuntu 16.04) in the directory /usr/share/nano. For example, you have a file related to syntax highlighting of shell scripts (sh.nanorc), Python scripts (python.nanorc), etc...

The syntax highlighting rules to be used is determined based on some pattern matching criteria specified in each file. Python files are matched based on the .py extension for instance.

When there is no match between the file characteristics (extension, interpreter line, etc...) nano uses the default file "default.nanorc".

This means that when you are opening a generic text file such as ~/.bashrc (for example) syntax highlighting is going to be performed using the regex rules contained in the file "default.nanorc". Just edit this file according to your required rules and then save it. Next time you open nano the changes are already stored and effective.

By the way, you can edit syntax highlighting for all other kind of files or the matching criteria between files and syntax highlighting rules by simply editing the corresponding *.nanorc file.

  • It can use the file's header (such as #!/bin/sh) and magic pattern (as used by the file utility) as well as just the extension, I think – steeldriver Feb 16 '18 at 20:59
  • @steeldriver Do you know how to do that? – wjandrea Feb 16 '18 at 20:59
  • 1
    @wjandrea not really - they look like regular expression matches on the shebang and the ASCII string representation of the file magic respectively, but I'm just basing that on the contents of the /usr/share/nano/*.nanorc files. For example the sh.nanorc file has header "^#!.*((ba|da|k|pdk)?sh[-0-9_]*|openrc-run|runscript)" and magic "(POSIX|Bourne.*) shell script text" – steeldriver Feb 16 '18 at 21:06
  • @steeldriver I added a shebang to my bashrc, now it's highlighted automatically! – wjandrea Feb 16 '18 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.