- What this tutorial is about ?
- What this tutorial is not about?
- Before you start
- Creating a live template of your own
- Using a live template of your own
- See also
- Other topics of this tutorial
PyCharm comes with quite a bit of the various live templates... However, there is only one pre-defined live template for Python. This tutorial aims to walk you through creating a live template for a Python class declaration, and using this live template.
Python programming is out of scope of this tutorial. Refer to the Python documentation for details.
The basics and usage of live templates are also not discussed here. You can find all the necessary information about the types, abbreviations, variables and storage of the live templates in the section Live Templates; to learn how to use the live templates, refer to the section Creating Code Constructs by Live Templates.
Make sure that you are working with PyCharm version 2.7 or higher. If you still do not have PyCharm, download it from this page. To install PyCharm, follow the instructions, depending on your platform.
Open the Settings dialog ( on the main toolbar, or Ctrl+Alt+S), and under the IDE Settings, click Live Templates:
Click . What happens next?
- First, note a new group with the name user.
- Second, under selected group there is a stub live template that by now is called <abbreviation>.
- Finally, the fields for entering template abbreviation, description, body text, and context appear:
So, let's enter the template abbreviation. In our example, we'll type the word class in the field Abbreviation, and then enter description (which is optional, but nevertheless...), and select the context, where our template should be applicable (in our case, this is Python):
The expansion key will be Tab, as specified by default.
Type the following code in the field Template text:
The portions of a template text enclosed in $ signs are the template variables. You can easily tell them from the entire template text, since they stand out with color:
These template variables are void yet, so let's define them first... All, except one (wait a bit to learn why).
Click the button Edit variables:
In the dialog box Edit template variables, you see the list of all (but one!) variables:
For the variable $object$, let's define its default value (object), and click OK:
You have already noticed that the template variable $END$ was not suggested for editing. This is because the template variable $END$ is pre-defined, and thus NOT EDITABLE. It always stands for the position of the cursor after the template expansion and filling in all the required fields. So in our case, the cursor will rest at the end of the new class declaration.
You only have to click OK in the Settings dialog. The new live template (or a code snippet) that expands into a stub Python class is ready. Let's just put it to a test...
First, create a Python file (Alt+Insert→Python file), and call it myPythonFile:
The new Python file opens for editing. Next, let's create a class declaration in it. To do that, type the template abbreviation class. Wow... our new live template is now in the suggestion list - you can tell it by description that we've typed just in case:
Press Tab to choose this option.
As expected, the abbreviation expands into a stub Python class. The red line marks the next entry point: when you type in the class name (variable class), it will be entered in this particular location:
Note, by the way, that the variable class has been used twice - in the class declaration, and in the comment for the class constructor. In this second case, the field for class has been filled in automatically.
Having typed the class name, press Enter, and see that the red line (frame) moves to the next field. Thus, you have to fill in all the required fields, and press Enter at the end:
Finally, the cursor rests at the end of the new class.
Learn more about using live templates in the section Creating Code Constructs by Live Templates.
You've passed one more tutorial - congrats! You have created a custom live template and then applied it to the source code.
- Python Live Templates for PyCharm
- Live Templates
- Live Templates page of the Settings dialog
- Creating Code Constructs by Live Templates