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 "\<(try|throw|catch|operator|new)\>" 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 "\<(case|do|done|elif|else|esac|exit|fi|for|function|if|in|local|read|return|select|shift|then|time|until|while)\>" color green "(\{|\}|\(|\)|\;|\]|\[|`|\\|\$|<|>|!|=|&|\|)" color green "-[Ldefgruwx]\>" color green "-(eq|ne|gt|lt|ge|le|s|n|z)\>" color brightblue "\<(cat|cd|chmod|chown|cp|echo|env|export|grep|install|let|ln|make|mkdir|mv|rm|sed|set|tar|touch|umask|unset)\>" icolor brightred "\$\{?[[email protected]#$*?-]+\}?" 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!
Thank you it was usefull! Keep a good work.
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!
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?
Oliver,
sudo nano /etc/nanorc
Be safe with sudo
@Neil
to fix the error
Replace
color yellow start=””
to
color yellow start=”” end=””
@Olivier
Use sudo nano /etc/nanorc
Thanks. I just make use of it.
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?