Sublime Text is a commonly-used text editor used to write Python code. Sublime Text’s slick user interface along with its numerous extensions for syntax highlighting, source file finding and analyzing code metrics make the editor more accessible to new programmers than some other applications like Vim and Emacs.
What makes Sublime Text awesome?
Sublime Text is often the first editor that newer programmers pick up because it works on all operating systems and it is far more approachable than Emacs, Vim or even PyCharm.
It is easy to get started in Sublime because the menus and options are accessible by using a mouse. There are no different modes to learn like Vim’s normal and insert modes. The keyboard shortcuts can be learned over time rather than all at once in the case of Vim or Emacs.
Sublime Text works well for beginners as soon as they install it and then can be extended with many of the features provided by an IDE like PyCharm as a developer’s skill level ramps up.
An additional bonus of using Sublime Text as a Python developer is that plugins are written in Python. Python developers can extend Sublime Text with their own programming language rather than learn a new language like Emacs’ Elisp or Vim’s Vimscript.
#1 SublimeLinter and Flake8 plugin
These packages are used to lint your code. Wondering what is code linting ?
A code linter is a program that analyses your source code for potential errors. The kinds of errors a linter can detect include:
- syntax errors;
- structural problems like the use of undefined variables.
- best practice or code style guideline violations.
pip3 install flake8
Then just to make sure flake8 is correctly installed — run flake8 badly_formatted_python_program.py and it will show you all the error in your program
Open package control by pressing ‘ctrl’ + ‘shift’ +’p’ and type ‘install package’ then click on the first result that appears. Then search for
and press ‘Enter’ to install. After installing ‘SublimeLinter, type this
SublimeLinter-flake8 and install it. Restart your Sublime and after the previous steps completed, we can see the linting errors and warnings right in Sublime Text and update live as we code.
#2 Python and Cython language bundles
THE MOST POPULAR AND MOST FEATURE RICH PYTHON SYNTAX HIGHLIGHTER FOR SUBLIME TEXT WITH OVER 125,000 DOWNLOADS AND BEING IN THE TOP 100 PACKAGES)!
Below are some of the most important improvements compared to the existing syntax highlighters:
- Added better number highlighting:
- All types of floating point notations are working now;
- All types of complex number notations are working now;
- New types of binary and octal-number notations are supported;
- Long integer is removed.
...syntax notation of
Ellipsisis supported now.
- Declaration rules are extended with
- Ex-statements-now-functions (like
- New Exception highlighting added.
- New keywords
- Function annotations are now supported.
- Conventional-language variable
- Optional-comment-based string highlighting:
- Better regex support (multiline, grouping, comments and more are improved).
- Format specifier mini-language
- Template strings
- Matrix multiplication operator
- String and byte literals:
- Byte notation added;
- Proper string prefixes added.
- All the unused built-in and magic functions/methods are now removed.
- All the unused keywords and notations are now removed.
Via Package Control
The fastest and easiest way to install these packages for Sublime Text is the following:
- Install Package Control
Package Control: Install Package
- Search for
Cython+packages and install them
- Happy coding 😉
Djaneiro supports Django templating and keyword highlighting and provides useful code snippets (tab completions) for Sublime Text. The snippet system is an incredible timesaver. You can create common Django blocks with only a few keystrokes for templates, models, forms, and views. Check out the official documentation to see a list of snippets. Docs-Djaneiro
I’ll admit I was skeptical at first when a friend of mine recommended Djaneiro to enhance my Django development workflow in Sublime Text.
I’d been happy with the Python development setup I built for myself over the years and I didn’t really understand what Djaneiro was going to add to that.
But when I tried out Djaneiro I was impressed how helpful it turned out to be!
Anaconda is ultimate python package because it adds a number of IDE like features to your Sublime Text Editor. To name a few, It includes
- Show Documentation
- McCabe code complexity checker
- Code Linting
- Goto Definition
Search for a package named
Anaconda and install it. I use a different linter package (SublimeLinter-flake8, which we installed in Step 1 of this post) so I disable linting altogether within the user-defined Anaconda settings file, Anaconda.sublime-settings, via the file menu: Sublime > Preferences > Package Settings > Anaconda > Settings – User:
If you have installed both PyYapf and Anaconda then the ‘ctrl’+’alt’+’f’ may not work because Anaconda uses this keybinding for the “finding all the usages of the selected word”. So, You have to change this shortcut to work with PyYapf. One way to do this is Go to Preferences > Package Settings > Anaconda > Key Bindings – Default and find command
anaconda_find_usages then change its key value from
#5 PyYapf Python formatter
Most of the current formatters for Python — e.g., autopep8, and pep8ify — are made to remove lint errors from code. This has some obvious limitations. For instance, code that conforms to the PEP 8 guidelines may not be reformatted. But it doesn’t mean that the code looks good. YAPF takes a different approach. It’s based off of ‘clang-format’, developed by Daniel Jasper. In essence, the algorithm takes the code and reformats it to the best formatting that conforms to the style guide, even if the original code didn’t violate the style guide. The idea is also similar to the ‘gofmt’ tool for the Go programming language: end all holy wars about formatting – if the whole codebase of a project is simply piped through YAPF whenever modifications are made, the style remains consistent throughout the project and there’s no point arguing about style in every code review. The ultimate goal is that the code YAPF produces is as good as the code that a programmer would write if they were following the style guide. It takes away some of the drudgery of maintaining your code
Sounds interesting? Let’s get started with the installation
Fire-up your terminal and install
pip3 install yapf
Then open Sublime Text and as always open package control > Install package and search for package named
PyYapf Python Formatter and install it.
Restart Sublime and select any poorly formatted python code and press ‘ctrl’+’alt’+’f’ and the code you selected should be reformatted according to the Python style guidelines. If you don’t select any line in and hit ‘ctrl’+’alt’+’f’ then your whole file will be reformatted (Cool Right ?)