Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 -*-
share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

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

nano --syntax=python myscript
share|improve this answer
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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