How to enable syntax highlighting for nano in Mac OS X 10.7 (Lion)?
According to what I found so far on Google is that it has got to do with /.nanorc
file. I have no idea how to get it or make it?
When I try to find nano
in my terminal this is what I get:
Notra:~ Sukhvir$ whereis nano
/usr/bin/nano
According to what I found on Internet this is the file I need to edit:
~/.nanorc
But how do I get to it/how to open it/if I don't have it then how to make it?
I am a bit new to programming folks, so step-by-step instructions will be highly appreciated.
I need it for C
mainly.
According to what I found online, I have to paste this into the .nanorc
file:
include "/usr/share/nano/nanorc.nanorc"
include "/usr/share/nano/c.nanorc"
However this will not work because there is no such directory as /usr/share/nano
.
I also just did ls /usr/share/
and according to the results there is no nano
in that directory. Is this a Mac OS X 10.7 (Lion) issue or an issue on my Mac?
On Mac, Homebrew ( brew
) will allow you to easily upgrade nano to a newer version than the one that came with Mac OSX.
Install brew
, then install a new version of nano
from the Terminal.
brew install nano
Installing this way includes the /usr/local/share/nano
folder containing the default syntax highlight files. You can now include "/usr/local/share/nano/c.nanorc"
in ~/.nanorc
.
Bonus: a run-once one-liner to add all languages.
/bin/ls /usr/local/share/nano/*.nanorc | xargs -I {} echo 'include "{}"' >> ~/.nanorc
Here are some steps to help you out.
/usr/local/share/
called 'nano' like this: mkdir /usr/local/share/nano
nano /usr/local/share/nano/c.nanorc`
http://code.google.com/p/nanosyntax/source/browse/trunk/syntax-nanorc/c.nanorc
nano ~/.nanorc
include "/usr/local/share/nano/c.nanorc"
Now, when you open up C files, you should see syntax highlighting. You can add additional syntax highlighting for different types of files using the same method. Just add more lines to your ~/.nanorc
file.
Note that depending on your user permissions, you may have to precede some of the above commands with sudo and then enter your root password.
I'm maintaining a bunch of fairly accurate syntax definitions for nano here: https://github.com/craigbarnes/nanorc . The default "example" definitions that come with nano are very poor quality, as are those mentioned above.
2018 Update
brew install nano
nano ~/.nanorc
include "/usr/local/Cellar/nano/*/share/nano/*.nanorc"
1st *
allows us to include whichever version of nano you have, instead of 2.9.3 (for example). Second *
includes all .nanorc files so we have syntax highlighting for all languages that are included!
Testing
cd ~/Desktop
touch test.py
touch test.js
nano test.py
nano test.js
Further .nanorc Customization
I added "set mouse" and "set smooth" to my .nanorc file as well. This allows for mouse use and smooth scrolling. Find more info on these options by running nano --help
.
Updated Syntax Highlighting
https://github.com/scopatz/nanorc has "improved" syntax highlighting, if desired.
brew update && brew install nano
After new nano is installed add this line to ~/.nanorc
to enable syntax highlighting for all provided syntaxes:
include /usr/local/share/nano/*.nanorc
As mentioned by Mark Mikofski in the comment to another answer, this is the maintained repo for nanorc
files:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.