How to Use Syntax Highlighting in Nano Editor

Nano is a simple and user-friendly command-line text editor for Linux, which is often used for basic text editing tasks, such as creating and editing configuration files, scripts, or simple documents.

However, many users don’t know that Nano supports syntax highlighting, which can make editing code files easier by coloring different parts of the text (like keywords, variables, and comments) for better visibility.

This guide will explain how to enable syntax highlighting in Nano to make your coding experience smoother and more efficient.

What Is Syntax Highlighting?

Syntax highlighting in a text editor means using different colors to represent different elements of a programming language.

For example, keywords may appear in bold or a different color, making code easier to read and debug. Nano can highlight syntax for many programming and scripting languages, such as Python, JavaScript, HTML, CSS, C, and Bash.

Installing Nano Editor in Linux

To use syntax highlighting, make sure you have a at least 2.0 or higher version of Nano installed. If you don’t have the required version, consider updating Nano through your package manager:

sudo apt install nano         [On Debian, Ubuntu and Mint]
sudo yum install nano         [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a sys-apps/nano  [On Gentoo Linux]
sudo apk add nano             [On Alpine Linux]
sudo pacman -S nano           [On Arch Linux]
sudo zypper install nano      [On OpenSUSE]    
sudo pkg install nano         [On FreeBSD]

Enabling Syntax Highlighting in Nano

Nano’s syntax highlighting rules are typically defined in configuration files located in the /usr/share/nano/ directory.

ls /usr/share/nano

You’ll see a variety of files, each corresponding to different programming languages or file formats. Examples include python.nanorc, c.nanorc, and html.nanorc.

Nano's Syntax Highlighting Configuration Files
Nano’s Syntax Highlighting Configuration Files

By default, Nano may not highlight syntax unless explicitly configured. To enable it, you need to modify your Nano configuration file, ~/.nanorc, and include the syntax files you need.

nano ~/.nanorc

Add the following lines to include the desired syntax highlighting rules:

include /usr/share/nano/python.nanorc
include /usr/share/nano/c.nanorc
include /usr/share/nano/html.nanorc

You can add more lines for different languages by replacing python.nanorc, c.nanorc, and html.nanorc with the appropriate filenames from the /usr/share/nano/ directory.

Enabling Syntax Highlighting in Nano
Enabling Syntax Highlighting in Nano

Save and exit Nano by pressing CTRL + X, then Y, and then ENTER.

Using Syntax Highlighting in Nano

Now, when you open a file in Nano that matches one of the configured syntax rules, you should see the text highlighted according to its syntax.

For example, if you open a Python file:

nano sample.py

You’ll notice that keywords, strings, and comments appear in different colors, making the code easier to read.

Python Code Highlighted in Nano
Python Code Highlighted in Nano

If you want to disable syntax highlighting temporarily, you can open Nano with the -Y option set to “none“:

nano -Ynone sample.py
Conclusion

Syntax highlighting can significantly improve your productivity when working with code in Nano. By enabling it and customizing rules to fit your needs, you can make Nano a more powerful tool for coding and editing configuration files.

Give it a try, and enjoy the colorful world of coding with Nano!

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

Ravi Saive
Ravi Saive
Administrator
About the author

I'm Ravi Saive, an award-winning entrepreneur and founder of several successful 5-figure online businesses, including TecMint.com, GeeksMint.com, UbuntuMint.com, and the premium learning hub Pro.Tecmint.com.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.