Spruce up the Nano text editor with syntax colouring and more

I use the Nano text editor for command line work. The version installed by Apple (2.0.6) is well behind the curve; use Brew to supersede it with the latest version (5.x at the time of editing).

Nano on macOS with syntax highlighting enabled — and much more

If you don’t have Homebrew installed, get the install string from brew.sh. Then run brew install nano. I also set up an alias in my .zshrc file to ensure the correct version gets used:

alias nano='/usr/local/bin/nano'

Update If you’re using Nano on an Apple Silicon Mac, your alias should read:

alias nano='/opt/homebrew/bin/nano'

Even then, Nano doesn’t quite sing, but by tweaking its startup file, .nanorc, which you’ll find in your home directory, you can make it much more tuneful.

Here’s mine:

set tabsize 4
set tabstospaces
set autoindent
set trimblanks
set linenumbers
set constantshow
set titlecolor white,red
set keycolor cyan
set functioncolor cyan
set numbercolor yellow
set mouse
include /usr/local/share/nano/*.nanorc

The first three lines are self-explanatory; I have them to force the use of spaces instead of tabs, and four spaces per tab. The fourth option, trimblanks, removes unecessary end-of-line whitespace when you save a file. These are vital for coding.

constantshow keeps the line count panel on screen. The four …color settings — formatted <foreground colour>,<background colour> — refer respectively to the title bar at the top of the editor screen; the list of key combinations and their meanings, at the bottom of the editing screen; and the colour of the line numbers. These are nice-to-have settings, but not essential.

Available names for the foreground and background colour include white, black, blue, green, red, cyan, yellow, magenta and normal — the latter is the default foreground or background colour. The name of the foreground colour may be prefixed with bright.

The last two lines are important. mouse allows you to position the cursor with the mouse — handy if you have a long file to get through. It can save a lot of scrolling or paging.

The include line makes use of 44 files installed in /usr/local/share/nano that provide syntax highlighting for a range of languages, including CSS, HTML, JavaScript, JSON, C, Objective-C, PHP, Python, Rust, Ruby, Shell and even .nanorc files themselves. Each file contains a list of regular expressions which Nano uses to colour key words when it opens files of the type(s) indicated by the file, so they can be readily used to create new files for new languages.

You can omit the final line on a Raspberry Pi as Nano is already set up for you, but otherwise you’re good to add or update your own ~/.nanorc file with the suggestions above. I have versions for each platform in my dotfiles repo and scripts to copy the correct version to my Mac or Pi home directory.

You can look up more .nanorc settings in the Nano documentation.

Further Reading

2 thoughts on “Spruce up the Nano text editor with syntax colouring and more

  1. John

    Thanks to duckduckgo for getting me here.

    I’m not using a Mac but Linux. This article helped me sort my nano setup nicely! The only thing I had to change on my Arch Linux .nanorc was
    include /usr/local/share/nano/*.nanorc

    should be :
    include /usr/share/nano/*.nanorc

    Thanks very much! This was a great help.

  2. The Apiarist

    Thanks for this … it’s worth noting that Homebrew seems to install to /opt/homebrew/bin/nano these days and the .nanorc files are lurking in /opt/homebrew/share/nano for the syntax highlighting provided by the final line of your suggested .nanorc file.

    Cheers
    David

Comments are closed.