I mostly use Eclipse IDE as my main development environment for Drupal, but have got into the habit of using nano (in emergency situations) to patch and edit my Drupal modules. Often I find it frustrating to with with nano without any kind of syntax highlighting, specially when trying to scan through a big module. A little googling around and I found a way to enable PHP syntax highlighting for nano (rather simple):
PHP syntax highlighting for nano:
Create a nano config file in your home folder
nano ~/.nanorc
add the following lines to .nanorc file
include "/usr/share/nano/php.nanorc" include "/usr/share/nano/sh.nanorc" include "/usr/share/nano/java.nanorc" include "/usr/share/nano/patch.nanorc" include "/usr/share/nano/python.nanorc" include "/usr/share/nano/html.nanorc" include "/usr/share/nano/perl.nanorc" include "/usr/share/nano/ruby.nanorc" include "/usr/share/nano/js.nanorc"
Note:
If you cannot find any of the included files in your /usr/share/nano/ folder, you can download/copy-paste the code from nanosyntax google code project and create a new syntax file like this:
sudo nano /usr/share/nano/php.nanorc
Paste the syntax code you copied from google project (link above)
OK, done. Now you have basic PHP Syntax Highlighting, but it still doesnt work for:
*.engine, *.theme, *.install, *.inc and *.module files...
Drupal specific syntax highlighting for nano:
We need to edit the php.nanorc file and add our additional drupal specific extensions so these are identified as PHP files:
sudo nano /usr/share/nano/php.nanorc
Add in additional file extensions like this:
syntax "php" "\.php[2345s~]?$" "\.module?$" "\.inc?$" "\.theme?$" "\.engine?$" "\.install?$"
Done, now PHP Syntax Formatting will work for all Drupal specific files, makes life a little easier.
Update: Custom Color Highlight for core Drupal functions
After a quick brainstorm with my friend and colleague Nick Thompson, we figured its possible to add custom color highlighting for core Drupal functions. Add a new color rule followed by functions names to your php.nanorc file, like so:
## Drupal functions ## change to a color of your choice and add pipe seperated function names color red "(drupal_bootstrap|drupal_goto|bootstrap_invoke_all|drupal_load)"
To get a list of all Drupal functions, you can run a command like this (thanks Nick):
grep "^function" * -hR | gawk '{ sub(/\(.+/, "", $2); print $2 }' | tr '\n' '|'Add the pipe seperated list of function names to the file above and its good to go.










7 comments
15th Aug, 09
this is a godsend. I have forgotten a lot of my vi and have been using nano a lot. Not so powerful, but growning own me. I can't ever seem to remember the search and replace commands.
Great all the same.
12th Dec, 09
I forgot how to do this, and this code really helped me.
Best Regards
Kamran
11th May, 11
That's not just the best awnser. It's the bestest answer!
28th Jun, 11
Brilliant! Thanks! Going to link your page on this topic on my website.
29th Jun, 11
no probs. Glad you found it useful. You have quite an informative blog yourself :)
1st Sep, 11
WOW! I was saying to myself: "Why can't we have code color in nano?"
So I put in Google: "nano code color"
Fifth link was this article and it is even more relevant to me since it's focused on Drupal!
I love when the "Collective intelligence" expresses itself like this!
Thank you Janak!
12th Nov, 11
Hi.
What a great tip. Was looking for nano syntax-highlight, and got a bonus regarding your aiming at Drupal development! - WIN WIN :)
But a can't see from where in the terminal, that I should type the command:
grep "^function" * -hR | gawk '{ sub(/\(.+/, "", $2); print $2 }' | tr '\n' '|'..- too get generated this long list of functions!?
Can you please tell me.
And thanks again!
Post new comment