Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Syntax highlighting in nano on Mac OS
# install Homebrew if you don't already have it: http://mxcl.github.io/homebrew/
# install nano from homebrew
brew install nano
# update your nanorc file with the contents of the nanorc file below
nano ~/.nanorc
# close and re-open your terminal and you'll have syntax highlighting
# ~/.nanorc
include "/usr/local/share/nano/*.nanorc"
@vladborovtsov

This comment has been minimized.

Copy link

@vladborovtsov vladborovtsov commented Feb 3, 2014

seems nano is not availbale in the homebrew anymore.

@iGeorgeX

This comment has been minimized.

Copy link

@iGeorgeX iGeorgeX commented Mar 1, 2014

Try "brew tap homebrew/dupes" and then "brew install nano" or better check https://github.com/Homebrew/homebrew-dupes

@StyxOfDynamite

This comment has been minimized.

Copy link

@StyxOfDynamite StyxOfDynamite commented May 1, 2014

Got this installed via homebrew/dupes but it seems as though I'm missing magic command ?

@Bazze

This comment has been minimized.

Copy link

@Bazze Bazze commented May 6, 2014

Yeah, I have the same issue. Missing the magic and header command. I'm looking for a solution but haven't found one yet, I'll let you know of any findings.

Command "magic" not understood
Command "header" not understood

@Bazze

This comment has been minimized.

Copy link

@Bazze Bazze commented May 6, 2014

Ha. Just after writing my comment I found the issue.

I had installed nano 2.3.2 via Homebrew which puts it in /usr/local/bin, which is fine. However, I did some fiddling with my PATH variable in my bash profile causing the /usr/bin directory to be higher up in the loading order which made the OS X default nano (version 2.0.6, which doesn't support colors) load instead of the local one.

@MelonSmasher

This comment has been minimized.

Copy link

@MelonSmasher MelonSmasher commented Sep 23, 2014

@Bazze this fixed it for me too.
.bash_profile:
export PATH=/usr/local/bin:$PATH

@michaelting

This comment has been minimized.

Copy link

@michaelting michaelting commented Oct 31, 2014

@MelonSmasher
If you don't want to modify the PATH variable (I have identical programs in both /usr/bin and /usr/local/bin, but I don't always want /usr/local/bin to take precedence, e.g. I want to use Python2.7 in /usr/bin but "export PATH=/usr/local/bin:$PATH" above forces me to use Python3 in /usr/local/bin), you can add this to .bash_profile instead:

alias my="PATH=/usr/local/bin:$PATH"

This way you can use both local/custom like so:

uses the nano in /usr/bin/nano, e.g. doesn't support color

$ nano myfile.txt

uses the colorized nano in /usr/local/bin/nano

$ my nano myfile.txt

If you want to default nano to the colorized one, you can also add this to .bash_profile:

always use colorized nano

alias nano="my nano"

so that the path doesn't strictly have /usr/local/bin higher up in loading order. Thus I could do:

Use Python2.7

$ python myfile.py

Use Python3

$ my python myfile.py

@sdd

This comment has been minimized.

Copy link

@sdd sdd commented Jan 14, 2015

@michaelting great tip, thanks

@abrahamrhoffman

This comment has been minimized.

Copy link

@abrahamrhoffman abrahamrhoffman commented Feb 19, 2015

@MelonSmasher - Thanks! That fixed it for me as well.

@leonklingele

This comment has been minimized.

Copy link

@leonklingele leonklingele commented Apr 27, 2015

Just add include /usr/local/share/nano/*.nanorcto ~/.nanorc

@floydpink

This comment has been minimized.

Copy link

@floydpink floydpink commented May 22, 2015

This is a good option too: https://github.com/scopatz/nanorc

@luckcraft

This comment has been minimized.

Copy link

@luckcraft luckcraft commented Feb 1, 2016

It's 2.5.1 version of nano, but still none of the workarounds have worked successfully.
No matter how I modify brew formula, i always get the same:

GNU nano, version 2.5.1
(C) 1999..2016 Free Software Foundation, Inc.
Email: nano@nano-editor.org Web: http://www.nano-editor.org/
Compiled options: --disable-libmagic --disable-nls --enable-utf8

Did smb figure out how to make configure flags work again?

@oefterdal

This comment has been minimized.

Copy link

@oefterdal oefterdal commented Feb 13, 2016

This did the trick for me :

Added
export PATH=/usr/local/bin:$PATH
to ~/.zshrc
And :
include /usr/local/share/nano/*.nanorc

to ~/.nanorc

@kritixilithos

This comment has been minimized.

Copy link

@kritixilithos kritixilithos commented Oct 17, 2016

I did

brew tap homebrew/dupes
brew install nano
nano ~/.nanorc

and edited the .nanorc file and it worked for me.

You can edit the syntax highlighting rules by editing the files at /usr/local/share/nano/

@Dids

This comment has been minimized.

Copy link

@Dids Dids commented May 7, 2017

Doesn't work on macOS Sierra anymore. :(

@bryantee

This comment has been minimized.

Copy link

@bryantee bryantee commented May 8, 2017

Unfortunately looks like no nano dir in /usr/local/share/ on my 10.12.4 install. Any updates to this for Sierra?

@lhaeberle

This comment has been minimized.

Copy link

@lhaeberle lhaeberle commented Jun 2, 2017

This is my macOS sierra work around:

  1. installed nano using brew: # this fails but find files in /usr/local/Cellar/nano/2.8.4/…
    brew install nano

  2. created an alias: # works well for me since i'm using some aliases
    alias nano='/usr/local/Cellar/nano/2.8.4/bin/nano --smooth --tabstospaces --linenumbers‘

  3. created file .nanorc in my (and su's) home-directory:

set mouse
include "/usr/local/Cellar/nano/2.8.4/share/nano/*.nanorc
@JMV2112

This comment has been minimized.

Copy link

@JMV2112 JMV2112 commented Oct 12, 2017

If anyone is still having either of these issues

Command "magic" not understood
Command "header" not understood

make sure to run

source ~/.bash_profile

to reload the PATH in your current session.

@tjt263

This comment has been minimized.

Copy link

@tjt263 tjt263 commented Feb 10, 2018

All you need to do once it's installed is brew --link nano.

@JBallin

This comment has been minimized.

Copy link

@JBallin JBallin commented Feb 12, 2018

I had issues with above solutions but laid out how I did it here: https://stackoverflow.com/a/48752837/4722345

@supriyo-biswas

This comment has been minimized.

Copy link

@supriyo-biswas supriyo-biswas commented Jun 1, 2018

It's also possible to use a wildcard instead of listing all the files:

include /usr/local/share/nano/*.nanorc
@chaim1221

This comment has been minimized.

Copy link

@chaim1221 chaim1221 commented Sep 20, 2018

yeah wildcard would have been helpful at the top, lol...

but all this talk about not working on High Sierra, it was literally as simple as the first two commands idk what y'all did

@brandonimitch

This comment has been minimized.

Copy link

@brandonimitch brandonimitch commented Feb 14, 2019

Doesn't work

@fevangelou

This comment has been minimized.

Copy link

@fevangelou fevangelou commented Mar 24, 2019

Thanks for the tips from the original post and the comments following.

Since Nano Editor v4.0 was released today, I made a bash script to simplify installation on the Mac, with syntax highlighting enabled.

https://gist.github.com/fevangelou/be744753730e86b8783fd481f311a7c9

Enjoy :)

@a3igner

This comment has been minimized.

Copy link

@a3igner a3igner commented Apr 10, 2019

I tried this but gets an error from nanorc

Cannot find directory

Include ~/.nanorc/Syntax

Even though it does exist! I tried every other possible way to write the include statement

What is wrong? I managed to find out after some digging and will answer this here in case other people have the same problem as me.

nano -V

are we running 4.0 or some old version of nano that comes with OSX?

sudo nano /etc/paths

does it look like this?

GNU nano 4.0 /etc/paths

/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin
~/Library/Python/2.7/bin/

then change the order and copy the row for /usr/local/bin to the top as such

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
~/Library/Python/2.7/bin/

Voila. now nano -V shows up as Vers 4.0 (use new terminal window). and all the syntax is loaded correctly.

@peterdk

This comment has been minimized.

Copy link

@peterdk peterdk commented Oct 13, 2019

Just installing from brew works perfectly here on 10.14.

@Julian-Nash

This comment has been minimized.

Copy link

@Julian-Nash Julian-Nash commented Feb 17, 2020

Create a ~/.nanorc file and paste the include list above.

In your ~/.bash_profile:

alias nano=./usr/local/bin/nano

Next time you open nano you should see the syntax highlighting

@aurimasniekis

This comment has been minimized.

Copy link

@aurimasniekis aurimasniekis commented Apr 16, 2020

Updated .nanorc file with latest syntaxes from brew

include /usr/local/share/nano/asm.nanorc
include /usr/local/share/nano/autoconf.nanorc
include /usr/local/share/nano/awk.nanorc
include /usr/local/share/nano/c.nanorc
include /usr/local/share/nano/changelog.nanorc
include /usr/local/share/nano/cmake.nanorc
include /usr/local/share/nano/css.nanorc
include /usr/local/share/nano/debian.nanorc
include /usr/local/share/nano/default.nanorc
include /usr/local/share/nano/elisp.nanorc
include /usr/local/share/nano/fortran.nanorc
include /usr/local/share/nano/gentoo.nanorc
include /usr/local/share/nano/go.nanorc
include /usr/local/share/nano/groff.nanorc
include /usr/local/share/nano/guile.nanorc
include /usr/local/share/nano/html.nanorc
include /usr/local/share/nano/java.nanorc
include /usr/local/share/nano/javascript.nanorc
include /usr/local/share/nano/json.nanorc
include /usr/local/share/nano/lua.nanorc
include /usr/local/share/nano/makefile.nanorc
include /usr/local/share/nano/man.nanorc
include /usr/local/share/nano/mgp.nanorc
include /usr/local/share/nano/mutt.nanorc
include /usr/local/share/nano/nanohelp.nanorc
include /usr/local/share/nano/nanorc.nanorc
include /usr/local/share/nano/nftables.nanorc
include /usr/local/share/nano/objc.nanorc
include /usr/local/share/nano/ocaml.nanorc
include /usr/local/share/nano/patch.nanorc
include /usr/local/share/nano/perl.nanorc
include /usr/local/share/nano/php.nanorc
include /usr/local/share/nano/po.nanorc
include /usr/local/share/nano/postgresql.nanorc
include /usr/local/share/nano/pov.nanorc
include /usr/local/share/nano/python.nanorc
include /usr/local/share/nano/ruby.nanorc
include /usr/local/share/nano/rust.nanorc
include /usr/local/share/nano/sh.nanorc
include /usr/local/share/nano/spec.nanorc
include /usr/local/share/nano/tcl.nanorc
include /usr/local/share/nano/tex.nanorc
include /usr/local/share/nano/texinfo.nanorc
include /usr/local/share/nano/xml.nanorc
@BlakeGardner

This comment has been minimized.

Copy link
Owner Author

@BlakeGardner BlakeGardner commented Apr 16, 2020

@aurimasniekis thanks, I've updated the gist to reflect the new file list

@valosekj

This comment has been minimized.

Copy link

@valosekj valosekj commented Nov 17, 2020

My set-up on MacOS Catalina (10.15.7) with zsh

Install nano from homebrew

brew install nano

After it, I have two versions of nano:

$ ls /usr/local/bin/nano
/usr/local/bin/nano           # installed from brew
$ ls /usr/bin/nano      
/usr/bin/nano                 # original one

Create alias to nano installed from brew inside my zsh config file:

$ sudo vim /etc/zshrc
alias nano='/usr/local/bin/nano'

Create nanorc file and include which languages you want to highlight:

nano ~/.nanorc
@docdyhr

This comment has been minimized.

Copy link

@docdyhr docdyhr commented Apr 29, 2021

nanorc: stable 2020.10.10, HEAD
Improved Nano Syntax Highlighting Files
https://github.com/scopatz/nanorc

brew install nanorc
include /usr/local/share/nanorc/Dockerfile.nanorc
include /usr/local/share/nanorc/Rnw.nanorc
include /usr/local/share/nanorc/apacheconf.nanorc
include /usr/local/share/nanorc/arduino.nanorc
include /usr/local/share/nanorc/asciidoc.nanorc
include /usr/local/share/nanorc/asm.nanorc
include /usr/local/share/nanorc/awk.nanorc
include /usr/local/share/nanorc/batch.nanorc
include /usr/local/share/nanorc/c.nanorc
include /usr/local/share/nanorc/clojure.nanorc
include /usr/local/share/nanorc/cmake.nanorc
include /usr/local/share/nanorc/coffeescript.nanorc
include /usr/local/share/nanorc/colortest.nanorc
include /usr/local/share/nanorc/conf.nanorc
include /usr/local/share/nanorc/conky.nanorc
include /usr/local/share/nanorc/creole.nanorc
include /usr/local/share/nanorc/csh.nanorc
include /usr/local/share/nanorc/csharp.nanorc
include /usr/local/share/nanorc/css.nanorc
include /usr/local/share/nanorc/csv.nanorc
include /usr/local/share/nanorc/cython.nanorc
include /usr/local/share/nanorc/dot.nanorc
include /usr/local/share/nanorc/dotenv.nanorc
include /usr/local/share/nanorc/elixir.nanorc
include /usr/local/share/nanorc/email.nanorc
include /usr/local/share/nanorc/erb.nanorc
include /usr/local/share/nanorc/etc-hosts.nanorc
include /usr/local/share/nanorc/fish.nanorc
include /usr/local/share/nanorc/fortran.nanorc
include /usr/local/share/nanorc/fsharp.nanorc
include /usr/local/share/nanorc/gemini.nanorc
include /usr/local/share/nanorc/genie.nanorc
include /usr/local/share/nanorc/gentoo.nanorc
include /usr/local/share/nanorc/git.nanorc
include /usr/local/share/nanorc/gitcommit.nanorc
include /usr/local/share/nanorc/glsl.nanorc
include /usr/local/share/nanorc/go.nanorc
include /usr/local/share/nanorc/gophermap.nanorc
include /usr/local/share/nanorc/gradle.nanorc
include /usr/local/share/nanorc/groff.nanorc
include /usr/local/share/nanorc/haml.nanorc
include /usr/local/share/nanorc/haskell.nanorc
include /usr/local/share/nanorc/hcl.nanorc
include /usr/local/share/nanorc/html.j2.nanorc
include /usr/local/share/nanorc/html.nanorc
include /usr/local/share/nanorc/i3.nanorc
include /usr/local/share/nanorc/ical.nanorc
include /usr/local/share/nanorc/ini.nanorc
include /usr/local/share/nanorc/inputrc.nanorc
include /usr/local/share/nanorc/jade.nanorc
include /usr/local/share/nanorc/java.nanorc
include /usr/local/share/nanorc/javascript.nanorc
include /usr/local/share/nanorc/js.nanorc
include /usr/local/share/nanorc/json.nanorc
include /usr/local/share/nanorc/keymap.nanorc
include /usr/local/share/nanorc/kickstart.nanorc
include /usr/local/share/nanorc/kotlin.nanorc
include /usr/local/share/nanorc/ledger.nanorc
include /usr/local/share/nanorc/lisp.nanorc
include /usr/local/share/nanorc/lua.nanorc
include /usr/local/share/nanorc/m3u.nanorc
include /usr/local/share/nanorc/makefile.nanorc
include /usr/local/share/nanorc/man.nanorc
include /usr/local/share/nanorc/markdown.nanorc
include /usr/local/share/nanorc/moonscript.nanorc
include /usr/local/share/nanorc/mpdconf.nanorc
include /usr/local/share/nanorc/mutt.nanorc
include /usr/local/share/nanorc/nanorc.nanorc
include /usr/local/share/nanorc/nginx.nanorc
include /usr/local/share/nanorc/nmap.nanorc
include /usr/local/share/nanorc/ocaml.nanorc
include /usr/local/share/nanorc/octave.nanorc
include /usr/local/share/nanorc/patch.nanorc
include /usr/local/share/nanorc/peg.nanorc
include /usr/local/share/nanorc/perl.nanorc
include /usr/local/share/nanorc/perl6.nanorc
include /usr/local/share/nanorc/php.nanorc
include /usr/local/share/nanorc/pkg-config.nanorc
include /usr/local/share/nanorc/pkgbuild.nanorc
include /usr/local/share/nanorc/po.nanorc
include /usr/local/share/nanorc/pov.nanorc
include /usr/local/share/nanorc/powershell.nanorc
include /usr/local/share/nanorc/privoxy.nanorc
include /usr/local/share/nanorc/prolog.nanorc
include /usr/local/share/nanorc/properties.nanorc
include /usr/local/share/nanorc/pug.nanorc
include /usr/local/share/nanorc/puppet.nanorc
include /usr/local/share/nanorc/python.nanorc
include /usr/local/share/nanorc/reST.nanorc
include /usr/local/share/nanorc/rpmspec.nanorc
include /usr/local/share/nanorc/ruby.nanorc
include /usr/local/share/nanorc/rust.nanorc
include /usr/local/share/nanorc/scala.nanorc
include /usr/local/share/nanorc/sed.nanorc
include /usr/local/share/nanorc/sh.nanorc
include /usr/local/share/nanorc/sieve.nanorc
include /usr/local/share/nanorc/sls.nanorc
include /usr/local/share/nanorc/sparql.nanorc
include /usr/local/share/nanorc/sql.nanorc
include /usr/local/share/nanorc/svn.nanorc
include /usr/local/share/nanorc/swift.nanorc
include /usr/local/share/nanorc/systemd.nanorc
include /usr/local/share/nanorc/tcl.nanorc
include /usr/local/share/nanorc/tex.nanorc
include /usr/local/share/nanorc/toml.nanorc
include /usr/local/share/nanorc/ts.nanorc
include /usr/local/share/nanorc/twig.nanorc
include /usr/local/share/nanorc/vala.nanorc
include /usr/local/share/nanorc/verilog.nanorc
include /usr/local/share/nanorc/vi.nanorc
include /usr/local/share/nanorc/x11basic.nanorc
include /usr/local/share/nanorc/xml.nanorc
include /usr/local/share/nanorc/xresources.nanorc
include /usr/local/share/nanorc/yaml.nanorc
include /usr/local/share/nanorc/yum.nanorc
include /usr/local/share/nanorc/zig.nanorc
include /usr/local/share/nanorc/zsh.nanorc
include /usr/local/share/nanorc/zshrc.nanorc
@BlakeGardner

This comment has been minimized.

Copy link
Owner Author

@BlakeGardner BlakeGardner commented Apr 29, 2021

@docdyhr I've updated the gist with your new list of includes

@atmonshi

This comment has been minimized.

Copy link

@atmonshi atmonshi commented May 8, 2021

thank you so much, worked perfectly for me on mac OS

@hacker1024

This comment has been minimized.

Copy link

@hacker1024 hacker1024 commented May 10, 2021

Why does every file need to be listed? The following works for me:

brew install nano
echo 'include "/usr/local/share/nano/*.nanorc"' >> ~/.nanorc
@lorthirk

This comment has been minimized.

Copy link

@lorthirk lorthirk commented May 31, 2021

+1 on @hacker1024 comment. single include with *.nanorc works for me as well.

@reginaliuyx

This comment has been minimized.

Copy link

@reginaliuyx reginaliuyx commented Jun 20, 2021

Thanks this worked! Is there any reason why the syntax highlighting only applies to nano, not sudo nano though?

@lorthirk

This comment has been minimized.

Copy link

@lorthirk lorthirk commented Jun 20, 2021

Thanks this worked! Is there any reason why the syntax highlighting only applies to nano, not sudo nano though?

In order to have it for sudo nano you have to edit the .nanorc for the root user

@reginaliuyx

This comment has been minimized.

Copy link

@reginaliuyx reginaliuyx commented Jun 20, 2021

@lorthirk That makes sense. Thanks for the help!

@romantech

This comment has been minimized.

Copy link

@romantech romantech commented Aug 22, 2021

Why does every file need to be listed? The following works for me:

brew install nano
echo 'include "/usr/local/share/nano/*.nanorc"' >> ~/.nanorc

This is nice! It works fine for me. Thank you!

@secfb

This comment has been minimized.

Copy link

@secfb secfb commented Oct 4, 2021

Mac OSX Big Sur
nano version:5.8

brew install nano 
echo 'include "/opt/homebrew/Cellar/nano/5.8/share/nano/*.nanorc"'>> ~/.nanorc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment