Turn on Python syntax highlighting in Mac OS

This is a very basic note to turn on syntax highlighting in Vim for Python coding.

Initial state looks like the following. All Python keywords are just displayed in one color; which obviously is not optimal when you write code.

screen-shot-2017-02-08-at-4-07-56-pm

To enable syntax highlighting when opening Python files using Vim, there are a couple of options to use.

  1. Editing the system vimrc file. This isn’t recommended and your changes will get overwritten anyway when you upgrade from one Mac OS version to another. You’ll also run into error messages complaining you’re trying to edit a read-only file. You can work around it but there’s a more straight forward approach below.
  2. Creating a new vimrc file in your home directory.

The system vimrc file is located in /usr/share/vim.

screen-shot-2017-02-08-at-4-11-47-pm

By default that is read-only as seen below. I’ll leave this file as it is and proceed to the other option.

screen-shot-2017-02-08-at-4-13-23-pm

Option 2 is better because you won’t have to mess with the system vimrc (i.e. you create a new vimrc file in your home directory)

To do that, just open a new terminal window and type in vim ~./vimrc

screen-shot-2017-02-08-at-4-11-47-pm

This will drop you in to your new vimrc file. Here you can add the lines:

syntax on
filetype plugin indent on

Syntax highlighting and indentation will now be enabled.

Esc then do an :echo $MYVIMRC command to see where the vimrc file you’re editing is located. It’ll show you your home directory. Do a :wq to save and exit from your Vim session.

The next time you open up one of your Python scripts in Vim, you’ll see keywords in color formatted mode as it should be.

screen-shot-2017-02-08-at-5-00-55-pm

Or you could just simply opt to use Sublime Text application for your Python codes.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s