Skip to main content

How to Configure Sublime Text 3 for Interactive Python Program Development

Programmers like to quibble about who has the 'best' development environment - what are the best tools? Should I use a text editor or a full-blown IDE? Which plugins and packages should I be using? These are all valid questions, but the bottom line is this: once you have chosen your preferred text editor/IDE you should spend a considerable amount of time configuring it and learning how to get the most out of it - this will save you time in the long run.

Please refer to my post on What programming Language and Text Editor / IDE To Chose for an easy and organised start for Non-technical guys.

My choice of text editor is Sublime Text 3. In this article I will give you a five-step tutorial on how to set up Sublime Text 3 for development with Python.

1. Installation

You can download and install Sublime Text 3 here.

After installation, the first thing you want to do is install the package manager. The package manager allows you to add and remove third-party plugins which will improve your development environment.

Open the Sublime console with CTRL + `. Then copy and paste the code from here into the console and hit enter. The package installer will take a few seconds to install. Afterwards, you can open up the command pallette with CTRL + SHIFT + P, and type package. A list of options should appear as follows:
package installer sublime text 3
If you see the package control options then you know you have installed it successfully!

2. Recommended Sublime Text 3 Plugins

One of the best things about Sublime Text is that developers can use 3rd party plugins to enhance their development environment. Hit `CTRL + SHIFT + P and select Package Control: Install Package. You can then search for the following plugins that I recommend you use for full-stack Python development:
2.1 Anaconda
Anaconda is an extremely powerful Python package for Sublime. It offers:
  • Python code autocompletion
  • Python linting (highlights both syntax errors and PEP8 violations)
  • Python documentation
  • McCabe complexity checker
  • Much more!
anaconda
2.2 Djaneiro
Djaneiro offers syntax highlighting for Django HTML templates, and tab autocompletion for Django Template Language syntax such as {% block %}{% endblock %}.
Once you have installed Djaneiro, open an HTML file and in the bottom-right corner select the language currently being used, then select Djaneiro -> HTML (Django) to ensure that Django Template syntax highlighting is applied:
Djaneiro syntax highlighting
The tab autocompletion means that you don't have to write out snippets of code over and over again. For example:
  • var + TAB generates {{ }}.
  • block + TAB generates {% block %}{% endblock %}
  • url + TAB generates {% url %}
  • static + TAB generates {% static %}
A full list of the tab autocompletions in Djaneiro is available here.
2.3 SideBar Enhancements
Most importantly, SideBar Enhancements sends files to your Recycle Bin if you delete a file from inside Sublime Text (instead of being sent into oblivion, never to be returned). It also offers a bunch of other features including being able to create new files from the sidebar.
2.4 Requirements Txt
This is a simple plugin which offers syntax highlighting for requirements.txt files. Use it!!
requirements.txt syntax highlighting
2.5 All AutoComplete
All AutoComplete "extends the default autocomplete to provide autocompletion for all open files".
2.6 GitGutter
GitGutter puts a margin into Sublime Text which indicates whether a line has been added, removed or edited. It is useful for tracking whether you have made changes to previous commits.
2.7 Other (JavaScript/CSS) Packages
There are a bunch of JavaScript/CSS packages which I also use for my full-stack Python Sublime Text 3 configuration. I won't go into quite so much detail on these, but you should still check them out:
  • AngularJS - offers AngularJS syntax highlighting and autocompletion.
  • jQuery - offers jQuery syntax highlighting and snippets.
  • Trimmer - package to delete trailing whitespace
  • CSS3 - CSS3 syntax highlighting and autocompletion
  • Sass - sass syntax highlighting and autocompletion
  • Color Highlighter - highlights hex, RGB and literal color strings in the appropriate color. For example blue in a CSS style sheet would be highlighted in blue so that you have a visual representation of the color.

3. Layout

The majority of layout options are available under the view heading in the menu bar.
In Views -> Columns you can switch the view to display 1, 2, 3, or 4 columns, 1 or 2 rows, or a grid of 2 * 2 sections. My preference is to use a 2 column layout.
two column layout sublime text
Other layout preferences that you might want to consider are as follows:
  • use CTRL + KB to toggle the sidebar (my preference is to keep this open at all times)
  • hit F11 to toggle full-screen mode, and hit SHIFT + F11 to toggle distraction free mode.
  • go to View -> Ruler to choose a right-margin to be displayed. I code according to PEP8 standards so set my right-margin at 80 characters.
  • go to View -> Hide Minimap to hide the minimap! I find it irritating.

4. Keyboard Shortcuts

It is extremely worthwhile spending a good chunk of time learning the Sublime Text keyboard shortcuts. The shortcuts that I use most frequently are listed below:
  • CTRL + K: deletes the current line.
  • CTRL + X: cuts the current line.
  • CTRL + SHIFT + UP moves highlighted text up.
  • CTRL + SHIFT + DOWN moves highlighted text down.
  • CTRL + W: closes the current tab.
  • CTRL + KK: deletes everything from the cursor until the end of the line.
  • CTRL + F: find.
  • CTRL + H: find and replace.
  • CTRL + KU: convert the selected text to uppercase.
  • CTRL + KL: convert the selected text to lowercase.
  • CTRL + KB: toggle the sidebar.
  • CTRL + [: unindent the current line.
  • CTRL + ]: indent the current line.
  • CTRL + M: jump to the closing bracket, or repeat to jump to the opening bracket.
  • CTRL + /: comment/uncomment the current line or selection.
HTML specific shortcuts:
  • ALT + .: completes the current HTML tag.
  • ALT + SHIFT + W: wrap in tag (defaults to <p>).
View specific shortcuts:
  • ALT + SHIFT + [NUMBER]: splits the view into [NUMBER] columns where [NUMBER] is 1-4.
  • ALT + SHIFT + 8: splits the view into a 2x2 grid.
  • CTRL + 0: focuses on the sidebar.
  • CTRL + 1-4: focuses on column 1-4.
  • CTRL + SHIFT + 1-4: moves the file to column 1-4.
A full list of Sublime Text 3 keyboard shortcuts is available here.

5. Console

When I first started programming in Python I used Enthought Canopy. This has an excellent built-in console IPython console by default. Configuring the console with ST3 is a little trickier, but well worthwhile.

Open the command palette using CTRL + SHIFT + P and select install package. Install the SublimeREPL package. Then go to Preferences -> Browse Packages -> SublimeREPL -> Config -> Python. Open the Main.sublime-menu file. Search for "id": "repl_python_run" using CTRL + F and add the option "-i" to the "cmd" section in the "args" below.



Then go to Tools -> Build System -> New Build System and enter the code from this StackOverflow answer and to facilitate, the same code has been reproduced below:
{
    "target": "run_existing_window_command", 
    "id": "repl_python_run",
    "file": "config/Python/Main.sublime-menu"
}
Caution: Please make sure that below mentioned code is replaced with above code under the New Build System.

{
    "shell_cmd": "make"
}
 

Save the file as Python-REPL.sublime-build. Then create a simple .py file, select your build system with Tools -> Build System -> Python-REPL and use CTRL + B to run your program. Variables from your program will now be available in the console that appears. Awesome!

What Next?

One small thing that currently irritates me with Sublime is that I can't get the CTRL + / comment/uncomment shortcut to work with .txt and .cfg files. If anybody knows how to do this, please get in touch!

Comments

Popular posts from this blog

Facts about being actuary !

জমির খতিয়ান কি কত প্রকার ও কি কি?

How to Run C and C++ Program in Sublime Text