Setting up Sublime Text for Python development
I recently started using Sublime Text 2 more and more as my main editor for Python development. This article explains my setup and some tweaks that make Python programmers happy.
Why Sublime Text?
I’ve been an avid user of TextMate for a long time. It’s light-weight, open-source, and as a native OS X application it feels very Mac-esque. While TextMate is a great editor it seems very bare bones sometimes.
For some projects I used the more beefy IntelliJ IDEA with the Python plug-in. I especially like its debugger and test runner. Yet, often a full-blown IDE like IntelliJ is overkill when working on small to medium-sized projects.
Over the last few weeks I began using Sublime Text more and more. Once I took the time to set it up I felt very much at home. It’s really fast, receives steady updates, and – as a big bonus – fully cross-platform. What finally won me over compared to TextMate was Sublime’s great plug-in ecosystem. There are several plug-ins available that make Python development very smooth and enjoyable.
I’m still switching editors on a per project basis now. But I noticed that for me Sublime Text seems to hit the sweet spot between a bare bones editor and a full-blown IDE for Python development.
Update: Is Sublime Text still the best choice for Python devs?
Since I wrote this article quite a few things have changed in the world of Python editors and IDEs. If you’re wondering whether Sublime Text is still the right choice for you then this review article I wrote may be helpful:
Font choice
Ubuntu Mono is a great font. I’ve switched from primarily using Menlo a few days ago and I’m not regretting it so far.
With Ubuntu Mono, I find font size 16 very comfortable to read on my 15-inch MacBook. At 1680 × 1050 the sidebar plus two editor views (wrapped at 80 characters) fit nicely next to each other.
If you want to go nuclear on making the ideal font choice, this topic on slant.co gives a good overview. It includes screenshots and download links for popular programming fonts.
Installed plug-ins
As mentioned before, Sublime has a very extensive plug-in ecosystem. I’m currently using the following plug-ins:
-
Package Control A package manager for installing additional plug-ins directly from within Sublime. This should be the only package you have to install manually. All other packages listed here can be installed via Package Control. It’s also possible to update installed packages with Package Control. Simply think of it as the
apt-get
of Sublime packages. -
Color Scheme - Tomorrow Night Color schemes determine the font colors used for syntax highlighting in the editor view. Tomorrow is a nice dark color scheme.
-
Theme - Soda Dark Themes change the color and style of Sublime’s UI elements. This one fits perfectly with the Tomorrow color scheme.
-
SideBarEnhancements This plug-in provides additional context menu options in the sidebar, such as “New file” or “New Folder”. These should be in there by default, but they are not.
-
All Autocomplete Sublime’s default autocomplete only considers words found in the current file. This plug-in extends the autocomplete word list to find matches across all open files.
-
SublimeCodeIntel Enhances autocomplete for some languages including Python. The plug-in also lets you jump to symbol definitions across files by pressing
alt
and then clicking on a symbol. Very handy. -
SublimeREPL Allows you to run a Python interpreter session in an editor view. I tend to use bpython in a separate terminal window but sometimes SublimeREPL is helpful.
-
GitGutter Adds little icons to the editor’s gutter area indicating whether a line has been inserted, modified, or deleted according to Git. To get colored icons update your color scheme file as instructed in the GitGutter readme.
-
Pylinter This plug-in provides the best pylint editor integration I’ve seen so far. It automatically lints
.py
files whenever they’re saved and displays pylint violations directly in the editor view. It also has a convenient shortcut that locally disables a pylint check by inserting a#pylint: disable
comment. This plug-in sealed the deal for me.
Preferences files
One of the nice things about Sublime Text is that it can be completely configured using simple JSON-based preferences files. This allows you to easily transfer your settings to another system. I’ve also seen people use Dropbox to automatically synchronize their settings on every computer they’re using.
Preferences.sublime-settings
configures Sublime’s look-and-feel and its built-in behavior. You can open the prefs file for editing within Sublime via Preferences > Settings – User. I’m using the following settings:
{ // Colors "color_scheme": "Packages/Tomorrow Color Schemes/Tomorrow-Night.tmTheme", "theme": "Soda Dark.sublime-theme", // Font "font_face": "Ubuntu Mono", "font_size": 16.0, "font_options": ["subpixel_antialias", "no_bold"], "line_padding_bottom": 0, "line_padding_top": 0, // Cursor style - no blinking and slightly wider than default "caret_style": "solid", "wide_caret": true, // Editor view look-and-feel "draw_white_space": "all", "fold_buttons": false, "highlight_line": true, "auto_complete": false, "show_minimap": false, "show_full_path": true, // Editor behavior "scroll_past_end": false, "highlight_modified_tabs": true, "find_selected_text": true, // Word wrapping - follow PEP 8 recommendations "rulers": [ 72, 79 ], "word_wrap": true, "wrap_width": 80, // Whitespace - no tabs, trimming, end files with \n "tab_size": 4, "translate_tabs_to_spaces": true, "trim_trailing_white_space_on_save": true, "ensure_newline_at_eof_on_save": true, // Sidebar - exclude distracting files and folders "file_exclude_patterns": [ ".DS_Store", "*.pid", "*.pyc" ], "folder_exclude_patterns": [ ".git", "__pycache__", "env", "env3" ] }
Pylinter.sublime-settings
configures the pylinter plug-in. I use the following settings to lint Python files automatically on save and to display graphical icons for lint violations:
{ // Configure pylint's behavior "pylint_rc": "/Users/daniel/dev/pylintrc", // Show different icons for errors, warnings, etc. "use_icons": true, // Automatically run Pylinter when saving a Python document "run_on_save": true, // Don't hide pylint messages when moving the cursor "message_stay": true }
Key bindings
Sublime’s key bindings are also fully user-configurable via JSON-based sublime-keymap
preferences files. I’ve made a few changes to the default bindings to better serve my existing TextMate/IntelliJ muscle memory. You may not need to make changes to the key bindings at all. But if you want to, modifying them is very easy and transferable across platforms. I use the following additional key bindings:
[ // Rebind "go to file" to cmd+shift+O { "keys": ["super+shift+o"], "command": "show_overlay", "args": { "overlay": "goto", "show_files": true }}, // Rebind swap line up/down to cmd+shift+up/down { "keys": ["super+shift+up"], "command": "swap_line_up" }, { "keys": ["super+shift+down"], "command": "swap_line_down" }, // Delete a line with cmd+delete { "keys": ["super+backspace"], "command": "run_macro_file", "args": { "file": "Packages/Default/Delete Line.sublime-macro" }}, // Reindent selection with cmd+alt+L { "keys": ["super+alt+l"], "command": "reindent"} ]
Command line tools
Similarly to TextMate’s mate
, Sublime Text includes a command line tool that allows you to open the editor from the shell. The tool called subl
is not enabled by default. To make it available from any shell do the following:
ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl
To use Sublime as the default editor for interactive Git commands, for example when composing commit messages, add the following line to your ~/.profile
:
export GIT_EDITOR="subl --wait --new-window"
I’ve recorded a quick screencast that shows you how to do to this in some more detail: » Using Sublime Text as your Git editor «
Further inspiration
I hope this little guide was helpful to you. If you’ve got any comments or suggested improvements, please feel free to drop me a line on Twitter or send an email. I’d like to thank the following authors for their articles on setting up Sublime. They inspired my setup and may teach you some more tricks as well: