Python Training by Dan Bader

Sublime Text Settings for Writing Clean Python

How to write beautiful and clean Python by tweaking your Sublime Text settings so that they make it easier to adhere to the PEP 8 style guide recommendations.

There are a few settings you can change to make it easier for you to write PEP 8 compliant Python with Sublime Text 3. PEP 8 is the most common Python style guide and widely used in the Python community.

The tweaks I describe in this article mainly deal with getting the placement of whitespace correct so that you don’t have to manage this (boring) aspect yourself.

I’ll also show you how to get visual indicators for the maximum allowed line-lengths in your editor window so that your lines can be concise and beautifully PEP 8 compliant—just like Guido wants them to be 🙂

Optional: Opening Sublime’s Syntax-Specific Settings for Python

The settings we’re changing now are specific to Python. Feel free to place them in your User settings, that will work just fine. However if you’d like to apply some or all of the settings in this tutorial only to Python code then here’s how you can do that:

  1. Open a Python file in Sublime Text (or create a new file, open the Command Palette and execute the “Set Syntax: Python” command)
  2. Click on Sublime Text → Preferences → Settings – More → Syntax Specific – User to open your Python-specific user settings. Make sure this opens a new editor tab called Python.sublime-settings. That’s the one you want!

If you’d like like to learn more about how Sublime Text’s preferences system works, then check out this tutorial I wrote.

Better Whitespace Handling

The following changes you can make to your (Syntax Specific) User Settings will help you keep the whitespace in your Python code clean and consistent:

"tab_size": 4,
"translate_tabs_to_spaces": true,
"trim_trailing_white_space_on_save": true,
"ensure_newline_at_eof_on_save": true

A tab_size of 4 is the general recommendation for writing Python. You’ll also want to enable translate_tabs_to_spaces to ensure that you don’t have a mixture of tabs and spaces in your Python files, which should be avoided.

The trim_trailing_white_space_on_save option will remove superfluous whitespace at the end of lines or on empty lines. I highly recommend enabling this because it can save headaches and merge conflicts when working with Git and other forms of source control.

PEP 8 recommends that Python files should end with a blank line to ensure that POSIX tools can process the file correctly. If you want to never have to worry about this again then turn on the ensure_newline_at_eof_on_save setting as this will make sure that your Python files end with a newline automatically.

Enable PEP 8 Line-Length Indicators

Another setting that’s really handy for writing PEP 8 compliant code is the “rulers” feature. It enables visual indicators in the editor area that show you the preferred maximum line length.

You can enable several rulers with different line lengths at the same time. This helps you follow the PEP 8 recommendations of limiting your docstrings to 72 characters and limiting all other lines to 79 characters.

Here’s how to set up the rulers feature for Python development. Open your (Syntax Specific) User Settings and add the following setting:

"rulers": [
    72,
    79
]

This will add two line-length indicators—one at 72 characters for docstrings, and one at 79 characters for regular lines. You can see them in the screenshot as vertical lines on the right-hand side of the editor area.

Turn On Word Wrapping

I like enabling Sublime’s word-wrapping feature when I’m writing Python. Most of my projects follow the PEP 8 style guide and therefore use a maximum line length of 79 characters.

I don’t want to get into an argument why that’s a good idea or not—but one benefit I found from limiting the lengths of my lines is that I can comfortably fit several files on my screen at once using Sublime’s “split layouts” feature.

This is especially useful if you’re following a test-heavy development process because you can see and edit the test and the production code at the same time.

Of course sometimes you’ll encounter a file that uses line-lengths above the 79 characters recommended by PEP 8. If I’m using split layouts with multiple editor panes at the same time it impacts my productivity if I have to scroll around horizontally.

The idea is to see all of the code at once. So, how can we fix that?

The best way I found to handle this is to enable Sublime’s word-wrap feature. This will visually break apart lines that are longer than the maximum line length. It might look a little odd sometimes but it’s still light years better than having to scroll around horizontally.

Here’s how you enable word wrapping. Open your (Syntax Specific) User Settings and add (or modify) the following options:

"word_wrap": true,
"wrap_width": 80

I’m setting the wrap_width to 80 which is one character past the 79 characters recommended by PEP 8. Therefore any line that goes beyond the PEP 8 recommendations will get wrapped.

<strong>5 Sublime Text Plugins</strong> to <em><u>Boost</u> Your Python Productivity</em>

5 Sublime Text Plugins to Boost Your Python Productivity

Free 5-day class—just enter your email address below:

This article was filed under: python, and sublimetext.

Related Articles:
Latest Articles:
← Browse All Articles