How To: Enable Code Syntax Highlighting for Python in the Nano Text Editor

Enable Code Syntax Highlighting for Python in the Nano Text Editor

With the plethora of programming that we promote at Null Byte, I figured a lot of you newbies that stop by IRC will need a big boost in the right direction to make your dive into programming a lot easier. A hot question that has been thrown around in IRC a lot when making the initial dive is, "How do I write code?". People who do not know much about computers can't understand how code is written. Code that we write here is usually scripted and written in Python, so we will use it as our example language.

Code can be written in any old text editor. Even Windows Notepad will do. I am writing this guide for Linux users because everyone who has been in the programming sessions have switched to Arch, or some other form of Linux, and Windows users can just use Notepad++.

You have a choice when it comes to how you want to type code. We can use a simple syntax highlighting editor (like we are preparing to do) or we could use an IDE (Intergrated Development Environment). An IDE has all sorts of features (code completion), but they tend to be bloated.

Today's Null Byte is going to focus on syntax highlighting. Syntax highlighting is a helpful feature that will color code certain significant words in a given programming language. This makes code easier to read and keep organized. Here is an example of what we are going to make the nano text editor appear as below when it displays Python code.

How to Enable Code Syntax Highlighting for Python in the Nano Text Editor

Requirements

Text in bold is a terminal command.

  • Installed copy of nano
        sudo pacman -S nano
  • Linux OS

Create a .nanorc File with Syntax Highlighting Scripts

  1. In your home directory, we need to create a .nanorc file, because it likely doesn't exist. If it does, just edit it.
        cd ~/ && nano .nanorc
  2. Open another terminal. We are going to use the default Python script for nano.
        cat /usr/share/nano/python.nanorc
  3. Paste the contents into your .nanorc file. You can also use this command.
        sudo echo $(cat /usr/share/python.nanorc) >>  ~/.nanorc
  4. Now, exit your terminal, and open another.
        nano test.py
  5. Start typing Python-related commands, like print and you will see them become highlighted. Pretty cool, eh?

Stop by the Null Byte IRC for group chat with friends! You can also catch Null Byte on G+ and Twitter.

2 Comments

I just recommend gedit. It is small, minimal, and has syntax highlighting built in with just a few clicks in the settings tab. Just my preference though.

Nano is the smallest it gets, and it
s terminal-based, so I love it ^_^. Gedit isn't as small as you would think, though. I don't like GUI text editors (besides SciTE), and if I need one, I made this nifty QT frontend for nano ^_^.

Share Your Thoughts

  • Hot
  • Latest