6

Turning on syntax highlighting in Nano is simple enough but I've just run into a non-standard issue. I have an executable Python script that doesn't have an extension.

It's part of a virtualenv environment so here's how it starts:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

2 Answers 2

9

You can select a specific syntax highlighting using the --syntax option, for example

nano --syntax=python myscript
1
2

Nano's default Python definition looks for one of two things an extension or a header match, as defined in /usr/share/nano/python.nanorc:

syntax "python" "\.py$"
header "^#!.*/python[-0-9._]*"

So this would match #!/bin/python. Unless you're writing something for the system, hard-coding the Python executable is a fairly bad idea... That's why env python is used these days.

The most simple way to fix Nano is to edit the definition by running sudoedit /usr/share/nano/python.nanorc and changing the header line to:

header "^#!.*python.*"

That is a lot more open.


There's actually a massively improved set of Syntax files available at the nanorc project on Github, that as well as improving other things, specifies:

header "^#!.*/(env +)?python[-0-9._]*( |$)"

It's no longer maintained but it's still a massive improvement over the defaults. It's fairly simple to install:

git clone https://github.com/nanorc/nanorc.git
cd nanorc
make install

Then nano ~/.nanorc and add:

include ~/.nano/syntax/ALL.nanorc

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .