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: Open a Python file in Sublime Text (or create a new file, open the Command Palette and execute the “Set Syntax: Python” command) 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.