35

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?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
sukhvir
  • 5,265
  • 6
  • 41
  • 43
  • You can edit the `~/.nanorc` by typing `nano ~/.nanorc`. Then add whatever you need to it to get highlight syntax working -- I assume that you've found the correct syntax in your Google searches for this. – CanSpice Mar 10 '12 at 00:10
  • thanks for the quick reply CanSpice , but like i just mentioned in my edited post the code I found online wont work because /usr/share/nano directory doen't exist on my mac. Any modifications I have to make to that code ??? – sukhvir Mar 10 '12 at 00:24

6 Answers6

67

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. See also the extra subdirectory.

To enable all default syntaxes, add a wildcard include to your ~/.nanorc.

include "/usr/local/share/nano/*.nanorc"

For Apple Silicon (M1), the path is under /opt/homebrew/:

include "/opt/homebrew/share/nano/*.nanorc"
Daemon
  • 103
  • 3
Joel Purra
  • 24,294
  • 8
  • 60
  • 60
  • This oneline is bash specific! – bot47 Apr 10 '14 at 16:55
  • 4
    Awesome! Also, I had to add this `export PATH=/usr/local/bin:$PATH` to my `.zshrc` to get it to work. – Sheharyar Dec 17 '14 at 10:11
  • 1
    Great instructions. I ran into a problem when running ```brew install nano```. The error: ```Error: SHA1 mismatch```. I simply removed the incomplete file (```rm -rf /dir/file```) that was located here: ```/Library/Caches/Homebrew/nano-2.2.6.tar.gz```. Did ```nano -V``` to get the version of nano already on my machine (2.0.6), then tried ```brew install nano``` again. Once I restarted my terminal it nano had been updated to 2.2.6. Thanks! – Ken Prince Dec 19 '14 at 23:29
  • 1
    Just to note that sometime prior to September 2017 the `homebrew/dupes` tap has been deprecated and the formulae (including nano) moved into the main repository, so a simple `brew install nano` is all that is required now. – davidA Sep 04 '17 at 22:28
  • @Sheharyar that is what I had to do to get this working as well. – james-see Sep 04 '19 at 14:09
26

Here are some steps to help you out.

  1. Create a new directory in /usr/local/share/ called 'nano' like this:

mkdir /usr/local/share/nano

  1. Now, using nano, make a nano resource file for your C syntax like this:

nano /usr/local/share/nano/c.nanorc`

  1. Now put your C code highlighting in this file and save it. Here is a link to some possible C syntax highlighting:

http://code.google.com/p/nanosyntax/source/browse/trunk/syntax-nanorc/c.nanorc

  1. Save that file and now open your user’s nano resource file by typing:

nano ~/.nanorc

  1. In this file, add a reference to the c.nanorc file you just made like this:

include "/usr/local/share/nano/c.nanorc"

  1. Save your user resource file.

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.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Jeff Fohl
  • 2,047
  • 2
  • 23
  • 26
11

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.

Craig Barnes
  • 111
  • 1
  • 2
  • +1 for very nice collection of definitions. Can you comment on how your collection compares with the one [here](https://github.com/scopatz/nanorc)? – Kevin A. Naudé Oct 03 '13 at 15:43
  • 4
    @CraigBarnes states "I am no longer maintaining this repo" There is an alternate repo of nanorc in [this answer](http://stackoverflow.com/a/19582587/1020470) and [Scopatz repo of nanorc](https://github.com/scopatz/nanorc) are good IMO; I use them on Mac. – Mark Mikofski Nov 01 '13 at 18:18
4

2018 Update

  1. Install Homebrew so you can download latest version of nano
  2. brew install nano
  3. nano ~/.nanorc
  4. Add file path to homebrew nano's syntax highlighting
    • (updated with new langs such as JS)
    • 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.

JBallin
  • 8,481
  • 4
  • 46
  • 51
1

Easy flow:

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

Community
  • 1
  • 1
Bijan
  • 25,559
  • 8
  • 79
  • 71
0

As mentioned by Mark Mikofski in the comment to another answer, this is the maintained repo for nanorc files:

https://github.com/scopatz/nanorc

Community
  • 1
  • 1
Hari Pachuveetil
  • 10,294
  • 3
  • 45
  • 68