HOWTO enable color for PHP and Bash scripts in Nano

— 15,912 views — 7 Comments

When working in shells, it’s very useful to have syntax highlighting. In vim it’s just ‘:sy on’ and you’re done. For those of you using GNU Nano, I’ll show you how to enable syntax highlighting as well.

To start, we’ll need some files which tell nano what to display in what color. Here is an example of such a file for PHP:

nano /usr/share/nano/php.nanorc

syntax “php” “.php|.inc$” color white start="<?(php)?" end="?>" color magenta start="<[^?]" end="[^?]>" color magenta “$[a-zA-Z_0-9]” color brightblue “->[a-zA-Z_0-9]” color cyan “([)|(])” color brightyellow “(var|class|function|echo|case|break|default|exit|switch|if|else|elseif|@|while|return|public|private|proteted|static)\s” color brightyellow “&lt;(try|throw|catch|operator|new)&gt;” color white “=” color green “[,{}()]” color green “==” color brightgreen “(’[^’]’)|("[^”]")" color yellow start="" color yellow start="/*" end="*/" color yellow start="#" end="$" color yellow “//.*"

Then we tell nano where this file is to be found:

nano /etc/nanorc

And add this line:

## PHP
include "/usr/share/nano/php.nanorc"

You then should see this when editing a PHP file with nano:

You can do the same for Bash files. Here’s the config file:

nano /usr/share/nano/sh.nanorc

syntax “sh” “.sh$” icolor brightgreen “^[0-9A-Z_]+()” color green “&lt;(case|do|done|elif|else|esac|exit|fi|for|function|if|in|local|read|return|select|shift|then|time|until|while)&gt;” color green “({|}|(|)|;|]|[|`|\|$|<|>|!|=|&||)” color green “-[Ldefgruwx]&gt;” color green “-(eq|ne|gt|lt|ge|le|s|n|z)&gt;” color brightblue “&lt;(cat|cd|chmod|chown|cp|echo|env|export|grep|install|let|ln|make|mkdir|mv|rm|sed|set|tar|touch|umask|unset)&gt;” icolor brightred “${?[0-9A-Z_!@#$?-]+}?” color cyan “(^|[[:space:]])#.$” color brightyellow “”(\.|[^”])"" “’(\.|[^’])’” color ,green “[[:space:]]+$"

It’s pretty easy to customize if you want. So now you never have to edit scripts without syntax highlighting again!

Comments

7 comments, archived as they were. New comments are closed.

Cosmin

Thank you it was usefull! Keep a good work.

Neil (SM)

Just came across this, and I see it’s a couple years old now — but thanks, was a big help!

One question — nano gives me an error on one line in the php file:
color yellow start=""

It didn’t like that there’s no end= section on that line. But I can’t figure out what you are aiming for there?? It looks like you’re using yellow for comments — and you seem to have that well-covered in the following lines. So I just removed it.

Thanks again!

Oliver

I can’t add the line:
include “/usr/share/nano/php.nanorc”

into:
nano /etc/nanorc

It responds with:
[ Error writing /etc/nanorc: Permission denied ]

Any Help?

The Man

Oliver,

sudo nano /etc/nanorc

Be safe with sudo

Rodrigo Valentim

@Neil
to fix the error

Replace
color yellow start=””
to
color yellow start=”” end=””

@Olivier
Use sudo nano /etc/nanorc

Joel Partida

Thanks. I just make use of it.

NearlyNormal

I have done this in /etc/nanorc and my /usr/share/nano/ folder has various files with the colors. Yet, Nano seems black and white. Is there a step I’m missing?