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

Nov 17, 2011 04:37 PM

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.

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.

Just updated your iPhone? You'll find new Apple Intelligence capabilities, sudoku puzzles, Camera Control enhancements, volume control limits, layered Voice Memo recordings, and other useful features. Find out what's new and changed on your iPhone with the iOS 18.2 update.

Comments

No Comments Exist

Be the first, drop a comment!